0
    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 文件上传到亚马逊 S3 存储桶,所有文件均已成功上传,但大文件(15 页 pdf 等)为空。Amazon transferManager - 正在通过 spring 注入。Amazon 凭证是从 TansferManager 中的 .property 文件注入的。注意:.png/.jpeg 文件也被上传为空文件。嗯,我有点困惑......发生了什么。需要一些输入。提前致谢。

4

2 回答 2

0

尝试使用此代码将您的 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");
        }
    }
}
于 2017-08-24T08:13:40.693 回答
0

通过 Streaming API尝试使用Apache Commons中的FileUpload 类。该页面上的代码片段可以正常工作。

于 2017-08-23T19:44:19.723 回答