Read the contents of a file from AWS s3 using its Pre-signed URL

Viewed 5345

I am trying to read and print the contents of a file from a s3 bucket using AWS Java Sdk. I have a presigned URL that lets me access (and download) the file. But, I am unable to read the file using the presigned-URL.

I am looking to do something similar to the code snippet below -

public void readFromS3(String bucketName, String key) throws IOException {
S3Object s3object = s3.getObject(new GetObjectRequest(bucketName, key));
System.out.println(s3object.getObjectMetadata().getContentType());
System.out.println(s3object.getObjectMetadata().getContentLength());

BufferedReader reader = new BufferedReader(new InputStreamReader(s3object.getObjectContent()));

String line;

while((line = reader.readLine()) != null) {
// can copy the content locally as well
// using a buffered writer

System.out.println(line);
}
}

The URL I have access to, lets me download the file.

I have also looked at the following reference with no success -

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/AmazonS3Client.html

http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/s3/model/GetObjectRequest.html

Can someone please help?

Thanks in advance!

3 Answers

You could very well do with TransferManager :)

String presignedURL = "presignedURL";
String targetDestination = "fileLocation"
    new TransferManager().download(new PresignedUrlDownloadRequest(new URL(presignedURL)),
            new File(targetDestination)).waitForCompletion();
Related