当前位置: 首页 > 面试题库 >

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

孟增
2023-03-14
问题内容

我尝试使用Amazon-
SDK(Java)示例代码S3TransferProgressSample.java将大文件上传到Amazon-S3存储(也在AWS文档中发布在此处)。

但是,当我尝试上传11 GB的文件时,上传会卡在不同的位置,并显示错误消息:

Unable to upload file to Amazon S3: Unable to upload part: Unable toexecute HTTP request: Unbuffered entity enclosing request can not be repeated " (attached screenshot).

看起来在发生IOException之后,SDK无法重试该请求(请参见下文)。

有人遇到吗?解决此问题的最佳实践是什么?任何代码表示赞赏。

 INFO: Received successful response: 200, AWS Request ID:
 2B66E7669E24DA75<br> Jan 15, 2011 6:44:46 AM
 com.amazonaws.http.HttpClient execute<br> INFO: Sending Request: PUT
 s3.amazonaws.com /test_file_upload/autogenerated.txt Parameters:
 (uploadId:
     m9MqxzD484Ys1nifnX._IzJBGbCFIoT_zBg0xdd6kkZ4TAtmcG0lXQOE.LeiSEuqn6NjcosIQLXJeKzSnKllmw--, partNumber: 1494, )<br> Jan 15, 2011 6:45:10 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     **INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error**<br>
     Jan 15, 2011 6:45:10 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     INFO: Retrying request<br> Jan 15, 2011 6:45:12 AM
     com.amazonaws.http.HttpClient execute<br> WARNING: Unable to execute
     HTTP request: Unbuffered entity enclosing request can not be
     repeated.<br> Jan 15, 2011 6:45:12 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     **INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error**<br>
     Jan 15, 2011 6:45:12 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     INFO: Retrying request<br> Jan 15, 2011 6:45:13 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     **INFO: I/O exception (java.net.SocketException) caught when processing request: Connection reset by peer: socket write error**<br>
     Jan 15, 2011 6:45:13 AM
     org.apache.commons.httpclient.HttpMethodDirector executeWithRetry<br>
     INFO: Retrying request<br> Jan 15, 2011 6:45:13 AM
     com.amazonaws.http.HttpClient execute<br>
     **WARNING: Unable to execute HTTP request: Unbuffered entity enclosing request can not be repeated.**<br> Jan 15, 2011 6:45:14 AM
     com.amazonaws.http.HttpClient execute<br> WARNING: Unable to execute
     HTTP request: Unbuffered entity enclosing request can not be
     repeated.<br> Jan 15, 2011 6:45:14 AM com.amazonaws.http.HttpClient
     execute<br> WARNING: Unable to execute HTTP request: Unbuffered entity
     enclosing request can not be repeated.<br> Jan 15, 2011 6:45:14 AM
     com.amazonaws.http.HttpClient execute<br> WARNING: Unable to execute
     HTTP request: Unbuffered entity enclosing request can not be
     repeated.<br> Jan 15, 2011 6:45:15 AM com.amazonaws.http.HttpClient
     execute<br> WARNING: Unable to execute HTTP request: Unbuffered entity
     enclosing request can not be repeated.<br> Jan 15, 2011 6:45:16 AM
     com.amazonaws.http.HttpClient execute<br> WARNING: Unable to execute
     HTTP request: Unbuffered entity enclosing request can not be
     repeated.<br> Jan 15, 2011 6:45:16 AM

 com.amazonaws.http.HttpClient
     execute<br> WARNING: Unable to execute HTTP request: Unbuffered entity
     enclosing request can not be repeated.<br> Jan 15, 2011 6:45:17 AM
     com.amazonaws.http.HttpClient execute<br> WARNING: Unable to execute
     HTTP request: Unbuffered entity enclosing request can not be
     repeated.<br> Jan 15, 2011 6:45:19 AM com.amazonaws.http.HttpClient
     execute<br> WARNING: Unable to execute HTTP request: Unbuffered entity
     enclosing request can not be repeated.<br> Jan 15, 2011 6:45:19 AM
     com.amazonaws.http.HttpClient execute<br> ....<br> Jan 15, 2011
     6:45:21 AM com.amazonaws.http.HttpClient handleResponse<br>
     **INFO: Received successful response: 204, AWS Request ID: E794B8FCA4C3D007**<br> Jan 15, 2011 6:45:21 AM
     com.amazonaws.http.HttpClient execute<br> ...<br> Jan 15, 2011 6:45:19
     AM com.amazonaws.http.HttpClient execute<br> INFO: Sending Request:
     DELETE s3.amazonaws.com /test_file_upload/autogenerated.txt
     Parameters:<br> ...<br> Jan 15, 2011 6:47:01 AM
     com.amazonaws.http.HttpClient handleErrorResponse<br> INFO: Received
     error response: Status Code: 404, AWS Request ID: 0CE25DFE767CC595,
     AWS Error Code: NoSuchUpload, AWS Error Message: The specified upload
     does not exist. The upload ID may be invalid, or the upload may have
     been aborted or completed.<br>

问题答案:

尝试使用底层API。

当出现问题时,这将使您有更多的控制权,因为它们可能与11GB文件有关。

往返S3的请求有时会失败。使用低级API,如果上传失败,您将可以重试其中的一部分。

重构一下Amazon文档中的示例:

// Step 2: Upload parts.
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++) {
    // Last part can be less than 5 MB. Adjust part size.
    partSize = Math.min(partSize, (contentLength - filePosition));

    // Create request to upload a part.
    UploadPartRequest uploadRequest = new UploadPartRequest()
                .withBucketName(existingBucketName).withKey(keyName)
                .withUploadId(initResponse.getUploadId()).withPartNumber(i)
                .withFileOffset(filePosition)
                .withFile(file)
                .withPartSize(partSize);

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

     filePosition += partSize;
}

   // Step 3: complete.
   CompleteMultipartUploadRequest compRequest = new 
                     CompleteMultipartUploadRequest(
                                existingBucketName, 
                                keyName, 
                                initResponse.getUploadId(), 
                                partETags);

   s3Client.completeMultipartUpload(compRequest);

注意:我不是Java开发人员,所以我可能会在语法上搞砸,但是希望这可以使您朝正确的方向前进。另外,如果上传反复失败,您将需要添加“重试计数器”以防止无限循环。



 类似资料:
  • 我们目前有一个小的web应用程序,其中一部分是文件上传。目前,我们在客户端上使用Plupload,并启用了分块功能,以允许上传大型文件。这些文件被保存在应用服务器上,当它们出现时,这些块被追加。 现在,我们正在使用AmazonS3来存储文件,并有可能使用多个应用服务器。我发现很难处理这些大块。我试着以他们为榜样,但我遇到了问题。我尝试的主要内容是这样的: 我的问题是我需要知道上传的块。当我从上传的

  • 首先让我指出到底发生了什么。 > 能够从本地工作站进行成功的快照和发布构建。工件也成功上传到nexus。 我们有许多团队/开发人员使用的中央hudson安装。从Hudson成功运行快照构建 针对特定项目的发布构建从Hudson失败,并出现以下错误: 〔信息〕上传:https://nlliprdcn28098.nl.eu.abnamro.com:8443/nexus/content/reposito

  • 本文向大家介绍PHP文件上传问题汇总(文件大小检测、大文件上传处理),包括了PHP文件上传问题汇总(文件大小检测、大文件上传处理)的使用技巧和注意事项,需要的朋友参考一下 由于涉及到本地和服务器两方面的安全问题,所以基于input type="file"形式的页面文件上传一直处于一个很尴尬的位置。一方面,用户不希望隐私泄露,所以浏览器无法对用户在上传时选择的文件做有效的判 断。另一方面,为了服务器

  • 此代码从您要在editText中注册的熟人处接收信息,然后单击finButton将接收到的信息保存为一个名为FriendList.txt的文件。但是,当按下finButton时,将从当前执行的try-catch语句中输出Toast消息。此外,checkpermission也不起作用,它包装在try~catch语句中,但在logcat上没有输出。 和显化。 洛克卡特

  • 问题内容: 我正在尝试使用PrintStream在Java文件中写入0xFF。当我使用十六进制编辑器打开其他值时,会将其他值正确写入文件,但是假定显示0xFF的值却改为0xC3BF。 使用的变量类型为int。经过几次尝试后,我还发现我可以输入的“最大值”值为0x7F,这将在十六进制编辑器中正确显示,如果我输入0x80,则十六进制编辑器将显示0xC280。 怎么了? 问题答案: 进行一些调查后发现,

  • 我上传一个文件到AmazonS3有问题。我开发了一个Grails RESTful服务,它使用AWS Java SDK生成预签名URL。当客户端上传一个文件时,它首先检索一个预签名的URL,然后使用这个将文件直接上传到我的S3 bucket。所以我有一个Grails服务,它创建了一个预签名的URL,如下所示...