Kelvin Ho
Kelvin Ho

Reputation: 45

Is it safe to cache AmazonS3 client for later use?

I am writing a Java program to upload files to AWS S3 and I have succeed to get the S3 client using the following code:

BasicAWSCredentials awsCreds = new BasicAWSCredentials("aaa", "bbb");
AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
    .withRegion(Regions.fromName("ccc"))
    .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).build();

As I find that it takes quite a few seconds each time to setup the S3 client, I am wondering if it is possible to cache the client for repeated use.

Also, if I cache the client for like a year, will the client still be valid to connect to AWS?

Upvotes: 3

Views: 1389

Answers (1)

Kannaiyan
Kannaiyan

Reputation: 13035

Your client will work as long as the the credentials are valid. It will work for a year if your credentials are not changed or updated.

Basically, when you create a client, you don't convert the original credentials to any form, everything will be reference later when needed to perform the actual operation.

Your client will no longer work once you update your credentials after its object creation.

If you want to initialize once and use it later for a year. Yes, it will work. With the best security practices, it is not good to keep the credentials fixed for a longer period of time.

More about credentials:

https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html

Hope it helps.

Upvotes: 2

Related Questions