4

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!

Ratnadeep
  • 123
  • 1
  • 11
  • "unable to read" is very vague problem statement. Are you getting any exceptions? If any, please add the exception stack. – kosa May 16 '17 at 13:55
  • 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? – Ratnadeep May 16 '17 at 14:00

3 Answers3

5

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())
kiiadi
  • 381
  • 2
  • 7
  • 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. – Thick_propheT May 16 '22 at 23:22
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.

Yeshodhan Kulkarni
  • 2,844
  • 1
  • 16
  • 19
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();
GRajaMca
  • 82
  • 1
  • 9