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.
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.
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.
The builders in the java AWS S3 sdk are generally not thread safe. So try not to use S3Client#getS3Client() in multi-threaded environment.
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 .
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With