SSLException由于亚马逊服务的上传项目

时间:2015-06-01 03:55:39

标签: android amazon-web-services sslexception

我将我的文件从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

亚马逊SDK使用它自己的客户端,应该从框中正确配置它。

这个行为的原因是什么?

2 个答案:

答案 0 :(得分:2)

SDK中存在一个错误,其中某些标题未经过签名。它会影响某些地区的S3,例如:法兰克福(欧洲中部-1)和中国(cn-north-1),需要sigv4。

AWS SDK for Android v2.2.2已发布http://aws.amazon.com/releasenotes/4067314458888112。此版本解决了S3的sigv4签名问题。请查看http://aws.amazon.com/mobile/sdk/

答案 1 :(得分:2)

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

我做了什么来解决它:

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

希望它有所帮助。