Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AmazonS3Client(credentials) is deprecated

I'm trying to read the files available on Amazon S3, as the question explains the problem. I couldn't find an alternative call for the deprecated constructor.

Here's the code:

private String AccessKeyID = "xxxxxxxxxxxxxxxxxxxx"; private String SecretAccessKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; private static String bucketName     = "documentcontainer"; private static String keyName     = "test"; //private static String uploadFileName    = "/PATH TO FILE WHICH WANT TO UPLOAD/abc.txt";  AWSCredentials credentials = new BasicAWSCredentials(AccessKeyID, SecretAccessKey);  void downloadfile() throws IOException {      // Problem lies here - AmazonS3Client is deprecated     AmazonS3 s3client = new AmazonS3Client(credentials);         try {         System.out.println("Downloading an object...");         S3Object s3object = s3client.getObject(new GetObjectRequest(                 bucketName, keyName));         System.out.println("Content-Type: "  +                 s3object.getObjectMetadata().getContentType());         InputStream input = s3object.getObjectContent();          BufferedReader reader = new BufferedReader(new InputStreamReader(input));         while (true) {             String line = reader.readLine();             if (line == null) break;              System.out.println("    " + line);         }         System.out.println();     } catch (AmazonServiceException ase) {           //do something     } catch (AmazonClientException ace) {         // do something     }  } 

Any help? If more explanation is needed please mention it. I have checked on the sample code provided in .zip file of SDK, and it's the same.

like image 961
Asif Ali Avatar asked Jan 31 '17 07:01

Asif Ali


People also ask

What is amazons3client?

Amazon S3 provides storage for the Internet, and is designed to make web-scale computing easier for developers. The Amazon S3 Java Client provides a simple interface that can be used to store and retrieve any amount of data, at any time, from anywhere on the web.

Do I need to close S3Object?

If you retrieve an S3Object, you should close this input stream as soon as possible, because the object contents aren't buffered in memory and stream directly from Amazon S3. Further, failure to close this stream can cause the request pool to become blocked.

Is Amazon S3 client thread safe Java?

The builders in the java AWS S3 sdk are generally not thread safe. So try not to use S3Client#getS3Client() in multi-threaded environment.

What is DefaultAWSCredentialsProviderChain?

public class DefaultAWSCredentialsProviderChain extends AWSCredentialsProviderChain. AWS credentials provider chain that looks for credentials in this order: Environment Variables - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY (RECOMMENDED since they are recognized by all the AWS SDKs and CLI except for .


1 Answers

You can either use AmazonS3ClientBuilder or AwsClientBuilder as alternatives.

For S3, simplest would be with AmazonS3ClientBuilder,

BasicAWSCredentials creds = new BasicAWSCredentials("access_key", "secret_key");  AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds)).build(); 
like image 188
franklinsijo Avatar answered Sep 22 '22 00:09

franklinsijo