Download the Amazon S3 file and get the URL

Is it possible to upload a txt / pdf / png file to Amazon S3 in one step and get the URL of the downloaded file as an answer. If so, is the AWS Java SDK a suitable library that I need to add to my java struts2 web application.

Please suggest me a solution for this.

+80
java amazon-s3 amazon-web-services
Jun 11 2018-12-12T00:
source share
7 answers

To make the file open before downloading, you can use the #withCannedAcl PutObjectRequest method:

 myAmazonS3Client.putObject(new PutObjectRequest('some-grails-bucket', 'somePath/someKey.jpg', new File('/Users/ben/Desktop/photo.jpg')).withCannedAcl(CannedAccessControlList.PublicRead)) 
-13
Oct 07
source share

No, you can’t get the URL in one action, but two :)

First of all, you may need to make the file public before downloading, because it makes no sense to get a URL that no one can access. You can do this by setting the ACL as suggested by Michael Astreiko. You can get the resource URL 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"); 

Note getResourceUrl difference between getResourceUrl and getUrl is that getResourceUrl will return zero when an exception occurs.

Note 2: the getUrl method getUrl not defined in the AmazonS3 interface. You must cast the object to AmazonS3Client if you use the standard linker.

+91
Jun 26 '15 at 17:58
source share

You can solve this problem yourself and specify the file name specified in the download request.

eg. if your bucket is mybucket and your file has the name myfilename :

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

The s3 bit will differ depending on what area your bucket is in. For example, I use the region of Southeast Asia, so my URLs are similar to:

 https://mybucket.s3-ap-southeast-1.amazonaws.com/myfilename 
+77
Jun 11 2018-12-12T00:
source share

Similarly, if you want a link through s3Client, you can use below.

 System.out.println("filelink: " + s3Client.getUrl("your_bucket_name", "your_file_key")); 
+4
May 25 '18 at 13:05
source share

a bit outdated, but still for those who stumble upon this in the future:

you can do this with a single line, assuming you have already written CredentialProvider and AmazonS3Client.

it will look like this:

  String ImageURL = String.valueOf(s3.getUrl( ConstantsAWS3.BUCKET_NAME, //The S3 Bucket To Upload To file.getName())); //The key for the uploaded object 

and if you haven't written CredentialProvider and AmazonS3Client, just add them before getting the URL, for example:

  CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider( getApplicationContext(), "POOL_ID", // Identity pool ID Regions.US_EAST_1 // Region ); 
+2
Aug 10 '17 at 2:49 on
source share

The answers from @hussachai and @Jeffrey Kemp are pretty good. But they have something in common: the return URL is made in the style of a virtual host, and not in the style of the path. For more information on s3 URL style, see AWS S3 URL Styles . In case some people want to have a path style, an s3 url is generated. Here is the step. Basically everything will be the same as @hussachai and @Jeffrey Kemp answers, with only one change to the string settings, as shown below:

 AmazonS3Client s3Client = (AmazonS3Client) AmazonS3ClientBuilder.standard() .withRegion("us-west-2") .withCredentials(DefaultAWSCredentialsProviderChain.getInstance()) .withPathStyleAccessEnabled(true) .build(); // Upload a file as a new object with ContentType and title specified. PutObjectRequest request = new PutObjectRequest(bucketName, stringObjKeyName, fileToUpload); s3Client.putObject(request); URL s3Url = s3Client.getUrl(bucketName, stringObjKeyName); logger.info("S3 url is " + s3Url.toExternalForm()); 

This will generate a URL like: https://s3.us-west-2.amazonaws.com/mybucket/myfilename

+1
Jul 23 '19 at 19:59
source share
  System.out.println("Link : " + s3Object.getObjectContent().getHttpRequest().getURI()); 

however, you can get a link to an already downloaded file in the S3 basket.

0
May 25 '18 at 7:23
source share



All Articles