繁体   English   中英

将文件上传到Amazon S3存储桶:上传效果很好,但是一些大文件为空

[英]Uploading file to Amazon S3 bucket : upload works fine but some large files are empty

    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 transferManager-正在通过春季注入。 Amazon凭据是从TansferManager中的.property文件注入的。 注意:.png / .jpeg文件也将作为空白上传。 嗯,我有点困惑...发生了什么事。 需要一些投入。 提前致谢。

尝试通过流API使用来自Apache Commons的 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");
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM