public void upload(List<CommonsMultipartFile> fileUpload) {
for(CommonsMultipartFile file : fileUpload)
{
try
{
String contentType = file.getContentType();
String newName = generateFileKey(file);
AmazonS3UploadRequest uploadRequest = new AmazonS3UploadRequest.Builder()
.bucket(bucket)
.contentType(contentType)
.newResourceName(newName)
.resourceStream(file.getInputStream(), Long.valueOf(file.getBytes().length))
.build();
uploadToS3(uploadRequest);
}
catch (Exception e)
{
LOG.error("Error while uploading files to s3: ", e);
throw new ServiceRuntimeException("Error writing file to s3 bucket");
}
}
}
public String uploadToS3(AmazonS3UploadRequest uploadRequest) {
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(uploadRequest.getResourceLength());
objectMetadata.setContentType(uploadRequest.getContentType());
Upload upload = transferManager.upload(uploadRequest.getBucket(), uploadRequest.getNewResourceName(), uploadRequest.getResourceStream(), objectMetadata);
}
我将pdf文件上传到amazon S3存储桶,所有文件都上传成功 但是大文件(15页pdf等)是空的。 亚马逊transferManager - 正在通过spring注入。 Amazon凭据是从TansferManager中的.property文件注入的。 注意:.png / .jpeg文件也被上传为空。 嗯,我有点困惑......发生了什么。需要一些投入。 提前谢谢。
答案 0 :(得分:0)
尝试使用 Apache Commons 中的FileUpload 类通过Streaming API。该页面上的代码段可以正常使用。
答案 1 :(得分:0)
尝试使用此代码将您的PDF上传到amazon s3,我假设您拥有AWS S3 app Id和密钥。
AWSCredentials credentials = new BasicAWSCredentials(appId, appSecret);
AmazonS3 s3Client = new AmazonS3Client(credentials);
String bucketPath = "YOUR_S3_BUCKET";
public void upload(List < CommonsMultipartFile > fileUpload) {
for (CommonsMultipartFile file: fileUpload) {
try {
String contentType = file.getContentType();
String pdfName = generateFileKey(file);
InputStream is = file.getInputStream();
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(is.available());
s3Client.putObject(new PutObjectRequest(bucketPath, pdfName, is, meta).withCannedAcl(CannedAccessControlList.PublicRead));
} catch (Exception e) {
LOG.error("Error while uploading files to s3: ", e);
throw new ServiceRuntimeException("Error writing file to s3 bucket");
}
}
}