0

I need to download objects from S3 bucket and I have below information to access the S3 bucket.

Access Key, Secret Key and Bucket End point. There is no region name.

I was using minio package to access the bucket and was able to access it using below code :

  public void getS3BucketObject() throws IOException, NoSuchAlgorithmException,
            InvalidKeyException, ServerException, InsufficientDataException, InternalException, InvalidResponseException,
            XmlParserException, ErrorResponseException {

        //creating minioClient to access S3 bucket
        minioClient =
                MinioClient.builder()
                        .endpoint(s3BucketEndpoint)
                        .credentials(s3BucketAccessKey, s3BucketSecretKey)
                        .build();

        //check for bucket existance
        boolean found =
                minioClient.bucketExists(BucketExistsArgs.builder().bucket(s3BucketName).build());
        if (!found) {
           System.out.println("Bucket doesn't exist ");
        } else {
          System.out.println("Bucket exists ");
        }
    }

But, I do need to use AWS SDK instead of minio but I am not sure as I don't have region information and not sure how to pass endpoint in the configuration setting, though I tried below.

 final BasicAWSCredentials credentials = new BasicAWSCredentials(s3BucketAccessKey, s3BucketSecretKey);
            final AmazonS3 s3client = AmazonS3ClientBuilder.standard()
                                                    .withCredentials(new AWSStaticCredentialsProvider(credentials));
                            
            boolean found = s3client.doesBucketExistV2(s3BucketName);

            if (found){
                System.out.println("The bucket is available ");
            }else {
                System.out.println("The bucket doesn't exist");
            }
Praveenks
  • 1,436
  • 9
  • 40
  • 79

1 Answers1

0

Have you tried using withRegion(AWS_S3_REGION) option while creating s3client using builder?

AmazonS3 s3client = AmazonS3ClientBuilder.standard()
 .withRegion("us-east-1") 
 .build();

Ref. https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3ClientBuilder.html

AmazonS3ClientBuilder.defaultClient() fails to account for region?

Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67