Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get an S3 object using a URL and the 2.x Java SDK?

I'm using the 2.x AWS Java SDK (https://docs.aws.amazon.com/sdk-for-java/index.html). I need to get an S3 object using the friendly HTTP URL (e.g. https://bucket.s3.region.amazonaws.com/key or https://s3.region.amazonaws.com/bucket/key).

The old SDK included an AmazonS3URI class that could parse a URL and extract the bucket and key. Does the 2.x SDK include similar functionality, or should I use Java's URI class to parse the URL?

like image 470
mrog Avatar asked Oct 23 '19 17:10

mrog


People also ask

How do I find my AWS S3 object URL?

In order to get the URL of an S3 Object via the AWS Console:Navigate to the AWS S3 console and click on your bucket's name. Use the search input to find the object if necessary. Click on the checkbox next to the object's name. Click on the Copy URL button.

How do I get my S3 upload URL?

You can get the resource URL either by calling getResourceUrl or getUrl . AmazonS3Client s3Client = (AmazonS3Client)AmazonS3ClientBuilder. defaultClient(); s3Client. putObject(new PutObjectRequest("your-bucket", "some-path/some-key.

How do I get pre signed URL S3?

Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/ . In the Buckets list, choose the name of the bucket that contains the object that you want a presigned URL for. In the Objects list, select the object that you want to create a presigned URL for.


2 Answers

This is what I did using URI from java.net:

URI uri = new URI(s3Url);

String bucketName = uri.getHost();

// remove the first "/"
String prefix = uri.getPath().substring(1);
like image 151
Bao Pham Avatar answered Sep 27 '22 19:09

Bao Pham


There isn't a way to do it with the SDK yet, but it might be available in the future. In the meantime, you can write your own code using Java's URI class, or use AmazonS3URI from the old SDK and hope it keeps working.

like image 45
mrog Avatar answered Sep 27 '22 20:09

mrog