2
votes

Below is my code

AmazonS3 s3  = new AmazonS3Client(credentials,config); // assume proper authentication

// generating URL for some bucket(XYZ) and key(test/abc.txt) 

java.util.Date expiration = new java.util.Date();
        expiration.setTime(6000*10*20);
        GeneratePresignedUrlRequest generateUrl = new GeneratePresignedUrlRequest("XYZ", "test/abc.txt");
        generateUrl.setMethod(HttpMethod.GET); // Default.
        generateUrl.setExpiration(expiration);
URL url = s3.generatePresignedUrl(generateUrl);

o/p of above code is
https://s3.amazonaws.com/XYX/test/abc.txt?AWSAccessKeyId=XXXXXXXXXXXXXXXXX&Expires=1200&Signature=YYYYYYYYYYYYYYYYY

Using above URL I am trying to download abc.txt but I am get error 403 (Server returned HTTP response code: 403 for URL:)

Please let me know how i can download files using URL from amazon s3.

1
could you please avoid duplicate and simple questions. - Albin Joseph
Please provide link of same question. :) - Sheroo Pratap

1 Answers

1
votes

Use HttpURLConnection you should be able to access the requested web page from java. Try the following code:

 HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
 httpcon.addRequestProperty("User-Agent", "YOUR_BROWSER_AGENT");