繁体   English   中英

Amazon S3存储桶大文件上传

[英]Amazon S3 Bucket Large File Upload

我试图使用Java SDK使用低级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;
    }
    Pleas make sure our AWS S3 configuration :
     <CORSConfiguration>
        <CORSRule>
            <AllowedOrigin>*</AllowedOrigin>
            <AllowedMethod>GET</AllowedMethod>
            <MaxAgeSeconds>3000</MaxAgeSeconds>
            <AllowedHeader>Authorization</AllowedHeader>
        </CORSRule>
    </CORSConfiguration>

您的上传之一失败。 您将需要从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);

此堆栈溢出问题包含用于改进示例错误处理的代码。

将大文件上传到Amazon S3时出现问题

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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