简体   繁体   English

在哪里可以找到适用于Android的Amazon S3图像上载代码的工作示例

[英]Where can I find WORKING samples of Amazon's S3 Image Upload Code for Android

I have spent the past 2 days struggling with Amazon's S3 SDK for Android. 在过去的两天里,我一直在努力寻找适用于Android的Amazon S3 SDK。 I was able to get the Java one (in Eclipse) working without any problems whatsoever; 我能够使Java语言(在Eclipse中)正常工作,而没有任何问题。 I could upload pictures, download them, and it would be no problem. 我可以上传图片,下载图片,这没问题。 Changing gears to Android, however, and I have had no luck. 但是,将齿轮换到Android上并没有运气。 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. 我使用了来自亚马逊的此链接: https : //aws.amazon.com/articles/SDKs/Android/3002109349624271如果没有,它对我完全没有用。 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). 如果您通过SDK链接指向“ Android示例代码”文件,则它们的github存储库(此处: https : //github.com/awslabs/aws-sdk-android-samples )包含关于上传文件(即图片)主题的零代码)。

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). (希望我知道为什么在Java / Eclipse中如此简单,而在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. PS:我的api_key在正确的资产文件夹中,我的凭据登录正确,该图像小于5mb,并且正在后台(异步)线程上运行,因此不在主线程上。

-Pat -拍

Have you tested to see that file:///storage/emulated/0/MyDir/image_1437585138776.jpg is a useable file? 您是否测试过发现file:///storage/emulated/0/MyDir/image_1437585138776.jpg是可用文件? A content URI is Android does not typically map to a file and is typically used with content resolvers. 内容URI是Android,通常不会映射到文件,通常与内容解析器一起使用。 I would double check that is actually a file path and not a content resolver uri path, which is what it looks like. 我会仔细检查这实际上是文件路径,而不是内容解析器uri路径,这就是它的样子。

If that pans out, then double check the internet connection of the device. 如果出现问题,请再次检查设备的互联网连接。 Can you call any AWS api? 您可以调用任何AWS API吗? Are you behind some kind of firewall or protected wifi? 您是否在某种防火墙或受保护的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). 最后,在Android应用程序中使用嵌入式证书并不安全(如果您只是在本地进行测试,这是可以理解的,但是从不发布带有嵌入式证书的应用程序,而是使用Amazon Cognito进行身份验证)。 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. 对于S3的其他示例,您可以参阅入门指南http://docs-aws.amazon.com/mobile/sdkforandroid/developerguide/getting-started-android.html ,其中包含许多S3示例。 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. GitHub上的S3示例(已于7月22日更新)提供了一个使用Transfer Utility的S3上传器示例,以及一个分步教程。

Hope that helps! 希望有帮助!

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. WestonE,您在帖子中提出了一些很棒的技巧,请确保不要将其用于具有本地凭据的产品中。 And to answer your Q, yes, surprisingly, file:///storage/emulated/0/MyDir/image_1437585138776.jpg is indeed a valid file. 为了回答您的问题,是的,令人惊讶的是, file:///storage/emulated/0/MyDir/image_1437585138776.jpg确实是有效的文件。 It may be as simple as the fact that HTC phones have really weird storage location systems. HTC手机的存储定位系统确实很奇怪,这很简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM