【问题标题】:Uploading file to Amazon S3 bucket : upload works fine but some large files are empty将文件上传到 Amazon S3 存储桶:上传工作正常,但一些大文件为空
【发布时间】:2017-08-24 16:13:40
【问题描述】:
    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 文件也被上传为空文件。 嗯,我有点困惑......发生了什么。需要一些输入。 提前致谢。

【问题讨论】:

    标签: java spring file-upload amazon-s3 s3-bucket


    【解决方案1】:

    通过 Streaming API 尝试使用 Apache Commons 中的 FileUploadclass。该页面上的代码 sn-p 可以正常工作。

    【讨论】:

      【解决方案2】:

      尝试使用此代码将您的 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");
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-04-12
        • 2014-11-11
        • 2018-04-04
        • 2015-05-05
        • 1970-01-01
        • 2018-04-25
        • 2017-09-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多