2016-01-19 105 views
0

嗨,我想保存圖像到s3,但我最近做了,而上傳圖像有一些警告顯示在控制檯上。如何解決它.. 如果圖像存在,我想要替換圖像保存圖像到aws s3,如果圖像存在替換圖像

public String saveImage(MultipartFile file, String imageName) { 
    try { 
     String bucketName = "bucket"; 
     AmazonS3 s3client = new AmazonS3Client(
       new BasicAWSCredentials("*****", "******")); 
     TransferManager transferManager = new TransferManager(s3client); 
     // Otherwise: (Service: Amazon S3; Status Code: 301; Error Code: 
     // PermanentRedirect) 
     // s3client.setRegion(Region.getRegion(Regions.EU_WEST_1)); 
     ObjectMetadata objectMetadata = new ObjectMetadata(); 
     objectMetadata.setContentDisposition("attachment; filename=" + file.getOriginalFilename()); 
     Upload upload = transferManager.upload(bucketName, imageName, file.getInputStream(), objectMetadata); 
     upload.waitForUploadResult(); 
     return "https://path-region/" + bucketName + "/" + imageName; 
    } catch (Exception e) { 
     return "You failed to upload " + e.getMessage(); 
    } 
} 




10:31:00,313 WARN [com.amazonaws.services.s3.AmazonS3Client] (s3-transfer-manag 
er-worker-1) No content length specified for stream data. Stream contents will 
be buffered in memory and could result in out of memory errors. 

回答

1

您可以在Spring中使用本教程的行動:取決於它。

private void saveImage(MultipartFile image) 
throws ImageUploadException { 
try { 
S3Bucket bucket = s3.getBucket("spittrImages"); 
S3Object imageObject = 
    new S3Object(image.getOriginalFilename()); 
imageObject.setDataInputStream(
image.getInputStream()); 
imageObject.setContentLength(image.getSize()); 
imageObject.setContentType(image.getContentType()); 
AccessControlList acl = new AccessControlList(); 
acl.setOwner(bucket.getOwner()); 
acl.grantPermission(GroupGrantee.ALL_USERS, 
Permission.PERMISSION_READ); 
imageObject.setAcl(acl); 
s3.putObject(bucket, imageObject); 
} catch (Exception e) { 
throw new ImageUploadException("Unable to save image", e); 
} 
}