Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon S3 upload file and get URL

Is it possible to upload a txt/pdf/png file to Amazon S3 in a single action, and get the uploaded file URL as the response?

If so, is AWS Java SDK the right library that I need to add in my java struts2 web application?

Please suggest me a solution for this.

like image 787
Sangram Anand Avatar asked Jun 11 '12 06:06

Sangram Anand


People also ask

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 my public URL After uploading pictures Galaxy S3?

Show activity on this post. Not sure on the exact command in C#, but in java you can use getResourceUrl() to get the uploaded file url. something similar will also be available in C#. Below method uploads file in a particular folder in a bucket and return the generated url of the file uploaded.

How do I upload files to Amazon S3?

In the Amazon S3 console, choose the bucket where you want to upload an object, choose Upload, and then choose Add Files. In the file selection dialog box, find the file that you want to upload, choose it, choose Open, and then choose Start Upload. You can watch the progress of the upload in the Transfer pane.


2 Answers

No you cannot get the URL in single action but two :)

First of all, you may have to make the file public before uploading because it makes no sense to get the URL that no one can access. You can do so by setting ACL as Michael Astreiko suggested. 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.jpg", new File("somePath/someKey.jpg")).withCannedAcl(CannedAccessControlList.PublicRead)) s3Client.getResourceUrl("your-bucket", "some-path/some-key.jpg"); 

Note1: The different between getResourceUrl and getUrl is that getResourceUrl will return null when exception occurs.

Note2: getUrl method is not defined in the AmazonS3 interface. You have to cast the object to AmazonS3Client if you use the standard builder.

like image 64
hussachai Avatar answered Sep 28 '22 04:09

hussachai


You can work it out for yourself given the bucket and the file name you specify in the upload request.

e.g. if your bucket is mybucket and your file is named myfilename:

https://mybucket.s3.amazonaws.com/myfilename 

The s3 bit will be different depending on which region your bucket is in. For example, I use the south-east asia region so my urls are like:

https://mybucket.s3-ap-southeast-1.amazonaws.com/myfilename 
like image 38
Jeffrey Kemp Avatar answered Sep 28 '22 04:09

Jeffrey Kemp