Search code examples
amazon-web-servicesamazon-s3aws-java-sdk

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


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!


Solution

  • If you have a pre-signed URL, you don't need to use the AWS sdk to access the S3 object.

    As @EricNord commented, The url itself provides the authentication with S3 to allow access. The URL will have an STS token appended to it in the query parameters, which enables authentication.

    A basic HTTP client will be able to read the contents of the URL.