@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String urlParam = req.getParameter("url"); String nameParam = req.getParameter("name"); AmazonS3Client amazonS3Client = new AmazonS3Client(new DefaultAWSCredentialsProviderChain()); URL url = new URL(urlParam); InputStream in = new BufferedInputStream(url.openStream()); ObjectMetadata objectMetadata = new ObjectMetadata(); objectMetadata.setContentType("image/jpeg"); PutObjectRequest putObjectRequest = new PutObjectRequest("wiseman-bucket-test", nameParam, in, objectMetadata); putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead); PutObjectResult result = amazonS3Client.putObject(putObjectRequest); resp.getWriter().print(amazonS3Client.getResourceUrl("wiseman-bucket-test", nameParam)); }
// Makes a putObjectRequest to make file public, and sends request. private static boolean putObject(AmazonS3 s3, PutObjectRequest req) { // Set file as public. req.withCannedAcl(CannedAccessControlList.PublicRead); // Send upload request. try { s3.putObject(req); } catch (AmazonServiceException e) { logger.severe(e.getMessage()); return false; } catch (AmazonClientException e) { logger.severe(e.getMessage()); return false; } return true; }