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

3

5

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!

Jacaranda answered 16/5, 2017 at 13:45 Comment(2)
"unable to read" is very vague problem statement. Are you getting any exceptions? If any, please add the exception stack.Smarm
My apologies for the vague statement. But, I don't know how to use the pre-signed Url to read the file. None of the GetObjectRequest constructors work with the URL. What are my options here? I don't want to download the file. Is there a way to get the bucketName and key from the presigned Url and use that to read the file?Jacaranda
D
3

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.

Deploy answered 16/5, 2017 at 14:49 Comment(1)
The url itself provides the authentication with s3 to allow accessQuintie
M
6

Using URLConnection is probably the simplest way, as others have pointed out it's just a regular HTTP URL at this point.

BufferedReader reader = new BufferedReader(new InputStreamReader(URI.create(presignedUrl).toURL().openConnection().getInputStream())
Michiko answered 16/5, 2017 at 22:52 Comment(1)
Thanks for the example. There are plenty of people answering questions like this one with "just make an HTTP request". As someone who primarily deals in desktop applications, idk how to make an HTTP request for a pre-signed url.Mongrelize
D
3

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.

Deploy answered 16/5, 2017 at 14:49 Comment(1)
The url itself provides the authentication with s3 to allow accessQuintie
S
0

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();
Stelle answered 20/8, 2021 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.