7

I upload my file to aws service from android. I configured it like this:

  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");
            }
        });
    }

And I got 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)

Happens on Android 4.3 and 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 uses it's own client and it should be configured properly from the box.

What is the reason for this beahaviour?

Android dev
  • 75
  • 1
  • 8
  • You got *what* SSLException? The stack trace should have been included in the question. – user207421 Jun 01 '15 at 04:42
  • It was added. You can see it if you scroll internal scroll view – Android dev Jun 01 '15 at 05:31
  • AWS Android SDK uses system's default SSL library. Would you please tell me about the API level, device or emulator, SDK version, and S3 region? – Yangfan Jun 01 '15 at 17:50
  • It is 5.1 but it should support a 4+ as well – Android dev Jun 02 '15 at 02:12
  • The region which I use is "cn-north-1" – Android dev Jun 02 '15 at 07:59
  • thank you for the information. I will try to reproduce it and get back to you asap. – Yangfan Jun 02 '15 at 08:37
  • I can't reproduce the problem. Some questions on StackOverflow said this error can be caused by shaky network. Is your device on Wifi or LTE? Have you tried to use AmazonS3Client directly? – Yangfan Jun 02 '15 at 23:51
  • I used WiFI, the same code for global bucket works fine. Yes, I have tried AmazonS3Client directly -- the same issue – Android dev Jun 03 '15 at 00:39
  • The issue is reproduced only for China region ( "cn-north-1"), for others buckets it works. Also the 4.3 and 4.4 versions of Android shows another exception. Please see update for original post – Android dev Jun 03 '15 at 06:19
  • We identified the root cause to that exception and we are working on a fix. Sorry about the trouble. – Yangfan Jun 03 '15 at 20:51

2 Answers2

2

There is a bug in the SDK where certain headers aren't signed. It will affect S3 in some regions, e.g. Frankfurt (eu-central-1) and China (cn-north-1), where sigv4 is required.

AWS SDK for Android v2.2.2 is out http://aws.amazon.com/releasenotes/4067314458888112. This release addresses the sigv4 signing issue with S3. Check it out at http://aws.amazon.com/mobile/sdk/.

Yangfan
  • 1,866
  • 1
  • 11
  • 13
  • Thank you for the update. Please keep me (and a whole community informed) – Android dev Jun 04 '15 at 04:51
  • Hi Yangfan, could you please clarify when we can expect the fix for this bug? I was told Amazon has releases on 2 weeks basis. Is it true? – Android dev Jun 05 '15 at 10:01
  • Please also take a look on this question http://stackoverflow.com/questions/30664295/amazons3exception-x-amz-website-redirect-location-header-is-not-supported-for-t I faced with issues with non-China bucket as well – Android dev Jun 05 '15 at 10:31
  • Not strictly on 2 week basis. This fix is likely done by next week. – Yangfan Jun 05 '15 at 17:22
  • Hi Yangfan, do you have a progress with patch to SDK? – Android dev Jun 11 '15 at 04:03
  • AWS SDK for Android v2.2.2 is out http://aws.amazon.com/releasenotes/4067314458888112. This release addresses an AWS signature version 4 signing issue with S3. Check it out at http://aws.amazon.com/mobile/sdk/. – Yangfan Jun 11 '15 at 23:43
  • Thank you, it works. Could you please suggest what is the meaning of "0" as a progress event code? It looks like this constant is not covered by SDK – Android dev Jun 16 '15 at 09:20
  • You can find the meaning of event code in the source https://github.com/aws/aws-sdk-android/blob/master/aws-android-sdk-core/src/main/java/com/amazonaws/event/ProgressEvent.java. "0" is the default, which doesn't match to a particular event. It could mean that the ProgressEvent object hasn't been updated yet. I think it's a bit off topic now. If you have further questions, please create a new one and tag it correctly. I will continue to help. Thank you. – Yangfan Jun 18 '15 at 04:56
  • Please take a look on https://github.com/aws/aws-sdk-android/issues/46, It was uploaded on Friday but were left without any reaction on it. – Android dev Jun 23 '15 at 06:23
  • Im having this problem too, Im using this: compile 'com.amazonaws:aws-android-sdk-core:2.+' compile 'com.amazonaws:aws-android-sdk-cognito:2.+' compile 'com.amazonaws:aws-android-sdk-s3:2.+' As you can see there is 2.* in each of the services used – cutiko May 02 '16 at 11:47
  • Me again, doing a follow up, figure what the problem is, is time difference problem. After messing with the ClientConfiguration for a while I thought it could be cause I have to change the hour manually on my phone (due some energy politics there was no time change this year), so I change the hour on the phone to what it should be. It worked. So there is the problem, how can I solve it without asking every user to change the hour on their phones? – cutiko May 02 '16 at 12:29
  • Hi, I have the latest sdk but still i face upload fail and similar issue. Please help !!!! – beginner May 06 '16 at 06:48
2

I was having the same problem on Android when trying to upload directly from the device to S3, and it was related to time difference issues on the devices, as cutiko said. I was using latest SDK version available (2.3.3), so the mentioned SDK fix didn't really solve my problem.

What I did to solve it:

  • Call an external service that returns the UTC time of an AWS server
  • Calculate the time difference between device time and server time, both in UTC
  • Use the s3Client.setTimeOffset() to set the time skew calculated before (in seconds)

Hope it helps.

SebaGra
  • 2,801
  • 2
  • 33
  • 43
  • Inspired by this, I changed my Android phone's Date and Time setting to `Automatic Time Update` and it solved my issue. Pretty sure AWS servers throw this error if there is a timestamp issue. Thank you Sir! – Anish Kumar Jun 27 '17 at 18:14