1

I have spent the past 2 days struggling with Amazon's S3 SDK for Android. I was able to get the Java one (in Eclipse) working without any problems whatsoever; I could upload pictures, download them, and it would be no problem. Changing gears to Android, however, and I have had no luck. Currently, with this selected code:

AmazonS3Client s3 = new AmazonS3Client( new BasicAWSCredentials(
        Constants.AWS_ACCESS_KEY, Constants.AWS_SECRET_ACCESS_KEY ) );
        //These are correct, I have already confirmed.

ObjectMetadata metaData = new ObjectMetadata();
metaData.setContentType("jpeg"); //binary data

PutObjectRequest putObjectRequest = new PutObjectRequest(
        Constants.BUCKETNAME, Constants.KEY3, new File(selectedImageUri.getPath())
); 
//selectedImageUri is correct as well, 
//(file:///storage/emulated/0/MyDir/image_1437585138776.jpg) 
putObjectRequest.setMetadata(metaData);

s3.putObject(putObjectRequest); //Errors out here

I am getting multiple errors, the most common of which is this:

AmazonHttpClient﹕ Unable to execute HTTP request: Write error: ssl=0xb8cefc10: I/O error during system call, Connection reset by peer
    javax.net.ssl.SSLException: Write error: ssl=0xb8cefc10: I/O error during system call, Connection reset by peer
            at com.android.org.conscrypt.NativeCrypto.SSL_write(Native Method)

I have done a ton of research and had no luck finding WORKING code. I used this link from Amazon: https://aws.amazon.com/articles/SDKs/Android/3002109349624271 Without it working for me at all. They say up top it is deprecated, but I cannot find any links to working code. If you follow the SDK links to 'android sample code' files, their github repo (here: https://github.com/awslabs/aws-sdk-android-samples) contains zero code on the topic of uploading files (namely pictures).

Does anyone have ANY idea where I can find some working code that shows how to just upload a stupid picture to my bucket?!??! (Wish I knew why this was so simple in Java/ Eclipse and not so in Android / Studio).

PS: I have my api_key in the correct assets folder, my credentials are correct for login, the image is under 5mb, and this is being run on a background (async) thread so as to not be on the main thread.

-Pat

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78
  • http://stackoverflow.com/questions/30565422/sslexception-due-uploading-item-for-amazon-service – Mircea Jul 22 '15 at 19:56
  • A good guess, but sadly, I went down that alley too. Looked into it and they fixed the bug last month :(. Thank you for helping me look though! – PGMacDesign Jul 22 '15 at 20:14
  • the ssl write failure still seems to be the issue here. Not sure the aws sdk is causing this. it may be a cert of library issue. Do you use keep-alive for your connections? What happens if you turn that off? – Mircea Jul 22 '15 at 20:18
  • Currently it is a really simple app that just makes that one call. I am not running any services or alarms to run it via a service. It is running on AsyncTask (So off the main thread). I am unsure of what the cert library issue is you mentioned is, I will have to research it a bit. / One thing I did figure out is that I think it is an issue before even the credentials hit. I changed the key and secret key to random letters and received the same error. Furthermore, I know those keys work as they upload just find via Eclipse/ Java. – PGMacDesign Jul 22 '15 at 21:09
  • yup. seems something goes wrong when the connection is established / tries to be established. – Mircea Jul 22 '15 at 21:16
  • Alrighty, that gives me somewhere to go. Thanks for helping me narrow it down Mircea, I appreciate it. I'll post back if I figure out the solution! – PGMacDesign Jul 22 '15 at 21:18

2 Answers2

1

Have you tested to see that file:///storage/emulated/0/MyDir/image_1437585138776.jpg is a useable file? A content URI is Android does not typically map to a file and is typically used with content resolvers. I would double check that is actually a file path and not a content resolver uri path, which is what it looks like.

If that pans out, then double check the internet connection of the device. Can you call any AWS api? Are you behind some kind of firewall or protected wifi?

Finally it is not secure to use embedded credentials in an Android app (understandable if you are just testing locally, but never ship an app with embedded credentials, instead use Amazon Cognito to authenticate). For other example with S3 you can see the Getting Started Guide http://docs-aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-android.html which has a bunch of S3 example. And the S3 sample on GitHub (which was updated on July 22nd) has an S3 uploader sample using the Transfer Utility, and a step-by-step tutorial along with it.

Hope that helps!

WestonE
  • 682
  • 3
  • 7
1

Figured out the answer, though the why still eludes me. Turns out, the issue was with the Buckets on the back-end. One of my buckets was renamed slightly (whitespace) which prevented any uploads to it. I deleted and recreated by bucket and then re-posted it, and it seemed to work just fine.

WestonE, you bring up some excellent tips in your post, making sure this does not go to production with local credentials is a very good call. And to answer your Q, yes, surprisingly, file:///storage/emulated/0/MyDir/image_1437585138776.jpg is indeed a valid file. It may be as simple as the fact that HTC phones have really weird storage location systems.

PGMacDesign
  • 6,092
  • 8
  • 41
  • 78