简体   繁体   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);
    }

I am uploading pdf file to amazon S3 bucket, all files are uploaded successfully but large files(15 pages pdf etc) are empty. 我正在将pdf文件上传到Amazon S3存储桶,所有文件都已成功上传,但是大文件(15页pdf等)为空。 Amazon transferManager - is being injected through spring. Amazon transferManager-正在通过春季注入。 Amazon credentials are injected from .property file in TansferManager. Amazon凭据是从TansferManager中的.property文件注入的。 Note : .png/.jpeg files are also being uploaded as empty. 注意:.png / .jpeg文件也将作为空白上传。 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. 尝试通过流API使用来自Apache Commons的 FileUpload 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. 尝试使用此代码将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.

相关问题 Java - 带有大括号“()”的 Amazon S3 存储桶上传问题未上传 - Java - Amazon S3 bucket upload issue with braces “()” not uploading 使用API​​在amazon S3存储桶中创建文件夹/上传文件 - creating a folder/uploading a file in amazon S3 bucket using API 在 Amazon S3 存储桶上上传文件时出现 NoSuchMethoError - Getting NoSuchMethoError While uploading file on Amazon S3 bucket 从Web浏览器上传大文件并传输到Amazon S3 - Uploading large files from web browser and transferring to Amazon S3 将大文件上传到 Amazon S3 时的问题 - Problems when uploading large files to Amazon S3 无法在eclipse中使用localhost将文件上传到Amazon S3存储桶 - Cannot upload a file to amazon S3 bucket with localhost in eclipse 使用java filechooser在Amazon s3存储桶中上传多个文件 - Upload multiple file in amazon s3 bucket using java filechooser 使用 Java 在 Amazon S3 上上传大尺寸(超过 1 GB)文件:大文件临时占用服务器中的大量空间 - Uploading large size (more than 1 GB) file on Amazon S3 using Java: Large files temporary consuming lot of space in server 将文件上传到 Amazon S3 存储桶。 使用适用于 Java v2 的 AWS 开发工具包 - Issuing uploading file to Amazon S3 Bucket. using the AWS SDK for Java v2 上传到Amazon S3时命名文件 - Naming files when uploading to Amazon S3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM