将文件上传到 Amazon S3 存储桶 :上传工作正常,但一些大文件为空


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等(是空的。 亚马逊转移管理器 - 正在注入春季。Amazon 凭证是从 TansferManager 中的 .property 文件注入的。 注意:.png/.jpeg文件也作为空文件上传。 嗯,我有点困惑...发生了什么事情。需要一些输入。 提前谢谢。

尝试使用Apache Commons 到 StreamingAPI 的 FileUpload。该页面上的代码片段将正常工作。

尝试使用此代码将您的 PDF 上传到 Amazon s3,我假设您有 AWS S3 应用程序 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");
}
}
}

最新更新