how to check if AmazonS3Client connection is active or not
Asked Answered
W

3

5

I am looking to develop a Singletone AmazonS3Client that serves my application to upload files to Amazon S3 server. however, I couldn't find how to check if the connection is active and able to upload buckets or not.

are there specific Exception that would be thrown if the connection dropped? or does the connection get dropped after certain period of time?

any answer would help.

here is snippet of my code:

private static final AmazonS3Client s3Client;
static {
  AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
  s3Client = new AmazonS3Client(awsCredentials);
}

private static boolean writeFile(AmazonS3Client s3Client, String fileName, File file, Boolean publicRead){
  try {
    PutObjectRequest p = new PutObjectRequest(bucketName, fileName, file);
    if (publicRead)
      p.setCannedAcl(CannedAccessControlList.PublicRead);
    s3Client.putObject(p);
    System.out.println("URL: " + S3_BASE_URL + fileName);
    return true;
  } catch (AmazonServiceException ase){
    ase.printStackTrace();
  } catch (AmazonClientException ace) {
    ace.printStackTrace();
  } catch (Exception ex){
    ex.printStackTrace();
  } catch (Throwable e) {
    e.printStackTrace();
  }
  return false;
}
Worrywart answered 7/12, 2014 at 10:55 Comment(1)
For reference you can just use catch (Exception ex){ ex.printStackTrace(); } as you do the same thing in all cases and it will catch all other exceptions as they inherit from ExceptionMorgan
A
5

Just in case someone else struggles with the same problem I stumbled upon, I post my experiences so far.

I tried with:

s3Client.doesBucketExist("my-bucket")

as it would fit my needs much better because I don't need to list all objects in the bucket, but just want to know if the entered bucket name is correct and if the credentials (s3 access key id and s3 secret access key) are correct.

The check for the bucket name worked as this approach will return true or false whether if the bucket exists or not but in case the credentials are wrong it will always return true which makes this check pretty useless.

Bug described here:

https://forums.aws.amazon.com/thread.jspa?threadID=77397

So I ended up adding the "ListBucket" Permission in IAM in the AWS Console and using

s3Client.listBuckets();

as this will fail with an exception if

  • bucket does not exist
  • access key id is invalid
  • secret access key is invalid

otherwise it will work.

Final code:

 try {
        final AmazonS3Client s3Client = new AmazonS3Client(new BasicAWSCredentials(s3AccessKeyId, s3SecretAccessKey));
        s3Client.listObjects(s3Bucket);
        return true;
    } catch (Exception ex) {
        return false;
    }
Annunciation answered 6/11, 2018 at 13:19 Comment(0)
E
3
List<Bucket> bucketList = null;
bucketList = s3Client.listBuckets();

If the bucketList size is null then connection is not active

Erymanthus answered 20/2, 2017 at 5:26 Comment(3)
Thanks for that @supriya ! My put object is failing, but listing buckets works. Been pulling my hair out for hours!Turnover
"If the bucketList size is null" -- did you mean "if the bucketList" is null, or if the size is zero? Because the size cannot be null.Continent
this worked well for me, thanks. In case someone can use the information, I would like to add that with Google Cloud Storage it's usually better to list the blobs in your bucket as users generally don't have the rights to list all buckets. in S3 the API returns only those buckets the user has the right to see.Aggrandize
S
3

In my case, I have pinged S3 with the following line:

amazonS3.listObjects(new ListObjectsRequest(s3Bucket, null, null, null, 1));

If there is any problem, it will throw an exception. The last number in the constructor is pretty important because it will bring only one element and not all the elements there.

Update: As Francesco said, using 0 as an argument works as well

Supply answered 10/6, 2022 at 16:29 Comment(1)
I have tested this approach with the newer ListObjectsV2Async method and it also works by passing 0 as the number of elements to retrieve.Fatigue

© 2022 - 2024 — McMap. All rights reserved.