Amazon S3 Bucket大文件上传

时间:2017-10-31 08:56:06

标签: amazon-web-services spring-boot amazon-s3 amazon-elastic-beanstalk

我正在尝试使用java SDK使用low-level api将大约5 Mb大小的CSV文件上传到Amazon S3。

我收到以下提到的错误:。

  

com.amazonaws.services.s3.model.AmazonS3Exception:指定的   上传不存在。上传ID可能无效或上传   可能已被中止或完成。 (服务:Amazon S3;状态代码:   404;错误代码:NoSuchUpload;请求ID:)

你能否提出一些可能出现问题的建议。我正在使用us-west-1区域。

    List<PartETag> partETags = new ArrayList<PartETag>();
    InitiateMultipartUploadRequest initRequest = new 
    InitiateMultipartUploadRequest(tempVariableBucketName, tempVariableAccessKey);
    InitiateMultipartUploadResult initResponse = s3Client.initiateMultipartUpload(initRequest);

    long contentLength = is.available();
    long partSize = 1 * 1024 * 1024; // Set part size to 1 MB.

    try {
        long filePosition = 0;
        for (int i = 1; filePosition < contentLength; i++) {
            partSize = Math.min(partSize, (contentLength - filePosition));
            logger.info("Upload Id " + initResponse.getUploadId());
            UploadPartRequest uploadRequest = new UploadPartRequest()
                .withBucketName(tempVariableBucketName).withKey(fileName)
                .withUploadId(initResponse.getUploadId()).withPartNumber(i)
                .withFileOffset(filePosition)
                .withInputStream(is)
                .withPartSize(partSize);

            partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
            filePosition += partSize;
        }
        CompleteMultipartUploadRequest compRequest = new CompleteMultipartUploadRequest(tempVariableBucketName,tempVariableAccessKey,initResponse.getUploadId(),partETags);
        s3Client.completeMultipartUpload(compRequest);
    } catch (Exception e) {
        logger.error(e.getMessage());
        s3Client.abortMultipartUpload(new AbortMultipartUploadRequest(tempVariableBucketName, tempVariableAccessKey, initResponse.getUploadId()));
        throw e;
    }

3 个答案:

答案 0 :(得分:0)

    Pleas make sure our AWS S3 configuration :
     <CORSConfiguration>
        <CORSRule>
            <AllowedOrigin>*</AllowedOrigin>
            <AllowedMethod>GET</AllowedMethod>
            <MaxAgeSeconds>3000</MaxAgeSeconds>
            <AllowedHeader>Authorization</AllowedHeader>
        </CORSRule>
    </CORSConfiguration>

答案 1 :(得分:0)

您上传的某个内容失败了。您需要从s3Client.uploadPart()中捕获错误并重试。

我建议对以下简单代码进行以下改进。

1)为每次重试添加一个增加的超时。

2)处理错误类型以确定重试是否有意义。对于某些错误,您应该只报告错误并中止。

3)将重试次数限制为10次以防止永远的while循环。

// repeat the upload until it succeeds.
boolean anotherPass;  
    do {
          anotherPass = false;  // assume everythings ok
          try {
              // Upload part and add response to our list.
              partETags.add(s3Client.uploadPart(uploadRequest).getPartETag());
          } catch (Exception e) {
                anotherPass = true; // repeat
          }
    } while (anotherPass);

此Stack Overflow问题包含用于改进示例的错误处理的代码。

Problems when uploading large files to Amazon S3

答案 2 :(得分:0)

我可以使用AWS的 GeneratePresignedUrlRequest 功能解决此问题。 但是现在我收到了一个新错误。 413请求实体太大了nginx 。我已经搜索了解决方案,我发现我需要在服务器的nginx.conf文件中进行更改。 现在问题出现了,因为我将有多个服务器/负载平衡器实例,所以,我是否必须为每个实例手动设置它?

相关问题