Verifique se existe um bucket - AWS SDKExemplos de código

Há mais AWS SDK exemplos disponíveis no GitHub repositório AWS Doc SDK Examples.

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

Verifique se existe um bucket

O exemplo de código a seguir mostra como verificar se existe um bucket.

Java
SDKpara Java 2.x
nota

Tem mais sobre GitHub. Encontre o exemplo completo e saiba como configurar e executar no Repositório de exemplos de código da AWS.

Você pode usar o doesBucketExists método a seguir como substituto do método SDK para Java V1 doesBucketExistAmazonS3Client# V2 (String).

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; } } }
  • Para API obter detalhes, consulte GetBucketAclem AWS SDK for Java 2.x APIReferência.