Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

615
Visualizações
How to check if bucket already exists in AWS S3

How can I check if the bucket already exists in my Aws S3 account using Java SDK?

Using below code

        AmazonS3ClientBuilder.defaultClient().doesBucketExistV2(bucketName);

Checks global existence of bucket and returns true if a bucket with this name exists globally even if I am not the owner of this bucket or I don't have access to that bucket.

I understand the intent for making this method this way so that It allows us to determine the availability of bucket name but this is not what I need. Of course, it will throw exception that I don't have access to it later, but it returns stating that bucket with this name exists.

I want to check if the bucket with the given name exists in my S3 account so that I can perform operations on it.

One possible solution for it can be to list all the buckets and search for my bucket in that returned list which I feel is not a good performance-wise (correct me if I am wrong) as there can be hundreds of thousands of buckets and searching in them is not efficient.

How can I determine if a bucket exists in my S3 account not checking global existence?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

A HeadBucket request does the job.

HeadBucketRequest headBucketRequest = HeadBucketRequest.builder()
            .bucket(bucketName)
            .build();

    try {
        s3Client.headBucket(headBucketRequest);
        return true;
    } catch (NoSuchBucketException e) {
        return false;
    }
over 4 years ago · Santiago Trujillo Relatório

0

SDK 1.x does not have a HeadBucketRequest builder nor a NoSuchBucketException.

In case anyone is looking for the answer using AWS SDK for Java 1.x, this works:

private boolean checkAccessToBucket(final String bucketName) {
    try {
        final HeadBucketRequest request = new HeadBucketRequest(bucketName);
        s3Client.headBucket(request);
        return true;
    } catch (AmazonServiceException ex) {
        if (ex.getStatusCode() == 404 | ex.getStatusCode() == 403 || ex.getStatusCode() == 301) {
            return false;
        }
        throw ex;
    } catch (Exception ex) {
        LOGGER.error("Cannot check access", ex);
        throw ex;
    }
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda