簡體   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