버킷이 있는지 확인 - AWS SDK 코드 예제

AWS Doc SDK ExamplesWord AWS SDK 리포지토리에는 더 많은 GitHub 예제가 있습니다.

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

버킷이 있는지 확인

다음 코드 예제는 버킷이 존재하는지 확인하는 방법을 보여줍니다.

Java
Java 2.x용 SDK
참고

더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요.

SDK for Java V1 AmazonS3Client#doesBucketExistV2(String) doesBucketExists 메서드를 대체하여 다음 메서드를 사용할 수 있습니다.

import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.awscore.exception.AwsServiceException; import software.amazon.awssdk.http.HttpStatusCode; import software.amazon.awssdk.services.s3.S3Client; import software.amazon.awssdk.utils.Validate; public class DoesBucketExist { private static final Logger logger = LoggerFactory.getLogger(DoesBucketExist.class); public static void main(String[] args) { DoesBucketExist doesBucketExist = new DoesBucketExist(); final S3Client s3SyncClient = S3Client.builder().build(); final String bucketName = "amzn-s3-demo-bucket"; // Change to the bucket name that you want to check. boolean exists = doesBucketExist.doesBucketExist(bucketName, s3SyncClient); logger.info("Bucket exists: {}", exists); } /** * Checks if the specified bucket exists. Amazon S3 buckets are named in a global namespace; use this method to * determine if a specified bucket name already exists, and therefore can't be used to create a new bucket. * <p> * Internally this method uses the <a * href="https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/s3/S3Client.html#getBucketAcl(java.util.function.Consumer)">S3Client.getBucketAcl(String)</a> * operation to determine whether the bucket exists. * <p> * This method is equivalent to the AWS SDK for Java V1's <a * href="https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html#doesBucketExistV2-java.lang.String-">AmazonS3Client#doesBucketExistV2(String)</a>. * * @param bucketName The name of the bucket to check. * @param s3SyncClient An <code>S3Client</code> instance. The method checks for the bucket in the AWS Region * configured on the instance. * @return The value true if the specified bucket exists in Amazon S3; the value false if there is no bucket in * Amazon S3 with that name. */ public boolean doesBucketExist(String bucketName, S3Client s3SyncClient) { try { Validate.notEmpty(bucketName, "The bucket name must not be null or an empty string.", ""); s3SyncClient.getBucketAcl(r -> r.bucket(bucketName)); return true; } catch (AwsServiceException ase) { // A redirect error or an AccessDenied exception means the bucket exists but it's not in this region // or we don't have permissions to it. if ((ase.statusCode() == HttpStatusCode.MOVED_PERMANENTLY) || "AccessDenied".equals(ase.awsErrorDetails().errorCode())) { return true; } if (ase.statusCode() == HttpStatusCode.NOT_FOUND) { return false; } throw ase; } } }
  • API 세부 정보는 GetBucketAcl AWS SDK for Java 2.x 참조의 API를 참조하세요.