Amazon Simple Storage Service
개발자 안내서 (API 버전 2006-03-01)

AWS SDK for Java를 사용하여 태그 관리

다음 예제는 AWS SDK for Java를 사용하여 새 객체에 대한 태그를 설정하고 기존 객체에 대한 태그를 가져오거나 대체하는 방법을 보여줍니다. 객체 태그 지정에 대한 자세한 정보는 객체 태그 지정 단원을 참조하십시오. 실제 예제를 작성 및 테스트하는 방법에 대한 자세한 내용은 Amazon S3 Java 코드 예제 테스트 단원을 참조하십시오.

// Copyright 2018, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 (For details, see import; import java.util.ArrayList; import java.util.List; import com.amazonaws.AmazonServiceException; import com.amazonaws.SdkClientException; import com.amazonaws.auth.profile.ProfileCredentialsProvider; import; import; import*; public class ManagingObjectTags { public static void main(String[] args) { String clientRegion = "*** Client region ***"; String bucketName = "*** Bucket name ***"; String keyName = "*** Object key ***"; String filePath = "*** File path ***"; try { AmazonS3 s3Client = AmazonS3ClientBuilder.standard() .withCredentials(new ProfileCredentialsProvider()) .withRegion(clientRegion) .build(); // Create an object, add two new tags, and upload the object to Amazon S3. PutObjectRequest putRequest = new PutObjectRequest(bucketName, keyName, new File(filePath)); List<Tag> tags = new ArrayList<Tag>(); tags.add(new Tag("Tag 1", "This is tag 1")); tags.add(new Tag("Tag 2", "This is tag 2")); putRequest.setTagging(new ObjectTagging(tags)); PutObjectResult putResult = s3Client.putObject(putRequest); // Retrieve the object's tags. GetObjectTaggingRequest getTaggingRequest = new GetObjectTaggingRequest(bucketName, keyName); GetObjectTaggingResult getTagsResult = s3Client.getObjectTagging(getTaggingRequest); // Replace the object's tags with two new tags. List<Tag> newTags = new ArrayList<Tag>(); newTags.add(new Tag("Tag 3", "This is tag 3")); newTags.add(new Tag("Tag 4", "This is tag 4")); s3Client.setObjectTagging(new SetObjectTaggingRequest(bucketName, keyName, new ObjectTagging(newTags))); } catch(AmazonServiceException e) { // The call was transmitted successfully, but Amazon S3 couldn't process // it, so it returned an error response. e.printStackTrace(); } catch(SdkClientException e) { // Amazon S3 couldn't be contacted for a response, or the client // couldn't parse the response from Amazon S3. e.printStackTrace(); } } }