重试将失败的文件上载到AWS S3

时间:2015-06-26 01:35:55

标签: java android amazon-web-services amazon-s3

我尝试使用TransferManager.upload(String bucket, String key, File file)从Android应用上传一个中等大小的文件(大约10 MB)到我的AWS S3存储桶。

以下代码间歇性地工作:

CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(...);
mTransfer = new TransferManager(credentialsProvider);
mTransfer.upload("bucket", key, file);

大约一半的时间,这很有效。另一半时间,由于SSLException导致上传失败。预计上传到S3会不可靠吗?我应该在客户端代码中处理重试吗?

06-25 18:27:50.326: I/AmazonHttpClient(10765): Unable to execute HTTP request: Write error: ssl=0xa1ef1c00: I/O error during system call, Connection reset by peer
06-25 18:27:50.326: I/AmazonHttpClient(10765): javax.net.ssl.SSLException: Write error: ssl=0xa1ef1c00: I/O error during system call, Connection reset by peer
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:794)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okio.Okio$1.write(Okio.java:73)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okio.RealBufferedSink.write(RealBufferedSink.java:44)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:310)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.android.okio.RealBufferedSink$1.write(RealBufferedSink.java:131)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.http.UrlHttpClient.write(UrlHttpClient.java:172)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.http.UrlHttpClient.writeContentToConnection(UrlHttpClient.java:129)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:65)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4234)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1644)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.uploadInOneChunk(UploadCallable.java:134)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.call(UploadCallable.java:126)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.upload(UploadMonitor.java:182)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(UploadMonitor.java:140)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(UploadMonitor.java:54)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
06-25 18:27:50.326: I/AmazonHttpClient(10765):  at java.lang.Thread.run(Thread.java:818)

1 个答案:

答案 0 :(得分:2)

不幸的是,这通常发生在网络非常不稳定时。您可能还会遇到无法执行HTTP请求:无法解析主机" YOURBUCKET.s3.amazonaws.com":没有与主机名关联的地址。遗憾的是,SDK无法做到这一点。它内置了自动重试功能,但在连接不稳定的情况下,这种情况可能会多次发生,最终SDK会放弃。您可以再次重试该请求,但如果这有助于或不符合网络强度。

韦斯顿