7

我将我的文件从 android 上传到 aws 服务。我是这样配置的:

  AwsMetadata awsMetadata = resultData.getParcelable(Params.CommandMessage.EXTRA_MESSAGE);
        AWSCredentials awsCredentials = new BasicAWSCredentials(
                awsMetadata.getAccountId(),
                awsMetadata.getSecretKey()
        );
        // set up region
        TransferManager transferManager = new TransferManager(awsCredentials);
        Region region = Region.getRegion(Regions.fromName(awsMetadata.getRegionEndpoint()));
        transferManager.getAmazonS3Client().setRegion(region);


        final MediaItem mediaItem = datasource.get(0);
        Log.d(App.TAG, "File is exists: "
                + mediaItem.getContentUri() + " "
                + new File(mediaItem.getContentUri()).exists());

        // prepare file for upload
        PutObjectRequest putObjectRequest = new PutObjectRequest(
                awsMetadata.getBucketName(),
                awsMetadata.getSecretKey(),
                new File(mediaItem.getContentUri())
        );


        Log.d(App.TAG, "Total data: " + mediaItem.getSize());
        Upload upload = transferManager.upload(putObjectRequest, new S3ProgressListener() {

            private int totalTransfered = 0;

            @Override
            public void onPersistableTransfer(PersistableTransfer persistableTransfer) {
            }

            @Override
            public void progressChanged(ProgressEvent progressEvent) {

                Log.d(App.TAG, "Bytes are transferred: " + progressEvent.getBytesTransferred());
                totalTransfered += progressEvent.getBytesTransferred();
                long totalSize = mediaItem.getSize();
                Log.d(App.TAG, "Total transferred: " + ((totalTransfered / totalSize) * 100) + " percent");
            }
        });
    }

我得到了 SSLException:

  06-01 11:45:00.712    5182-5768/com.home I/AmazonHttpClient﹕ Unable to execute HTTP request: Write error: ssl=0xb4bb3600: I/O error during system call, Connection reset by peer
        javax.net.ssl.SSLException: Write error: ssl=0xb4bb3600: I/O error during system call, Connection reset by peer
                at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)
                at com.android.org.conscrypt.OpenSSLSocketImpl$SSLOutputStream.write(OpenSSLSocketImpl.java:765)
                at com.android.okio.Okio$1.write(Okio.java:70)
                at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
                at com.android.okio.RealBufferedSink.write(RealBufferedSink.java:44)
                at com.android.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:291)
                at com.android.okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:116)
                at com.android.okio.RealBufferedSink$1.write(RealBufferedSink.java:131)
                at com.amazonaws.http.UrlHttpClient.write(UrlHttpClient.java:155)
                at com.amazonaws.http.UrlHttpClient.createConnection(UrlHttpClient.java:143)
                at com.amazonaws.http.UrlHttpClient.execute(UrlHttpClient.java:60)
                at com.amazonaws.http.AmazonHttpClient.executeHelper(AmazonHttpClient.java:353)
                at com.amazonaws.http.AmazonHttpClient.execute(AmazonHttpClient.java:196)
                at com.amazonaws.services.s3.AmazonS3Client.invoke(AmazonS3Client.java:4234)
                at com.amazonaws.services.s3.AmazonS3Client.putObject(AmazonS3Client.java:1644)
                at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.uploadInOneChunk(UploadCallable.java:134)
                at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadCallable.call(UploadCallable.java:126)
                at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.upload(UploadMonitor.java:182)
                at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(UploadMonitor.java:140)
                at com.amazonaws.mobileconnectors.s3.transfermanager.internal.UploadMonitor.call(UploadMonitor.java:54)
                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                at java.lang.Thread.run(Thread.java:818)

发生在 Android 4.3 和 4.4 上

com.amazonaws.services.s3.model.AmazonS3Exception: There were headers present in the request which were not signed (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: 06CAF94ADEFE242E), S3 Extended Request ID: ouMiu+15fshPnT9uz95T3Drj+Gea3gI1c+Rj34BhcSCzbIH2ypOeK9yvIlNCxbxt

Amazon SDK 使用它自己的客户端,并且应该从盒子中正确配置。

这种行为的原因是什么?

4

2 回答 2

2

SDK 中有一个错误,其中某些标头未签名。它将影响某些需要 sigv4 的地区的 S3,例如法兰克福 (eu-central-1) 和中国 (cn-north-1)。

适用于 Android v2.2.2 的 AWS 开发工具包已发布http://aws.amazon.com/releasenotes/4067314458888112。此版本解决了 S3 的 sigv4 签名问题。在http://aws.amazon.com/mobile/sdk/上查看。

于 2015-06-03T21:07:47.820 回答
2

尝试直接从设备上传到 S3 时,我在 Android 上遇到了同样的问题,正如 cutiko 所说,这与设备上的时差问题有关。我使用的是最新的 SDK 版本(2.3.3),所以提到的 SDK 修复并没有真正解决我的问题。

我做了什么来解决它:

  • 调用返回 AWS 服务器的 UTC 时间的外部服务
  • 计算设备时间和服务器时间之间的时间差,均以 UTC 为单位
  • 使用s3Client.setTimeOffset()设置之前计算的时间偏差(以秒为单位)

希望能帮助到你。

于 2016-11-17T11:02:20.313 回答