简体   繁体   中英

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);
    }

I am uploading pdf file to amazon S3 bucket, all files are uploaded successfully but large files(15 pages pdf etc) are empty. Amazon transferManager - is being injected through spring. Amazon credentials are injected from .property file in TansferManager. Note : .png/.jpeg files are also being uploaded as empty. Hmm I am kind of confused...what's happening. need some inputs. Thanks in advance.

Try with FileUpload class from Apache Commons thru Streaming API. The code snippet on that page will work fine.

Try this code to upload your PDF to amazon s3, I assume that you have AWS S3 app Id and secret key.

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");
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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