简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM