java 从android简单上传到S3
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31999551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple upload to S3 from android
提问by Aizen
I've searched the web for ways to upload a simple file to s3 from android but couldn't find anything that work and I think it is because lack of concrete steps.
我在网上搜索了从 android 上传一个简单文件到 s3 的方法,但找不到任何有效的方法,我认为这是因为缺乏具体步骤。
1.) https://mobile.awsblog.com/post/Tx1V588RKX5XPQB/TransferManager-for-Android
1.) https://mobile.awsblog.com/post/Tx1V588RKX5XPQB/TransferManager-for-Android
That is the best guide I found but it does not tell what libraries to include. I downloaded the aws sdk for android and inside there were many jars. So intuitively, I included the aws-android-sdk-s3-2.2.5.jar however that is not complete. It does not have the class BasicAWSCredentials.
这是我找到的最好的指南,但它没有说明要包含哪些库。我下载了适用于 android 的 aws sdk,里面有很多 jars。很直观,我包含了 aws-android-sdk-s3-2.2.5.jar 但这并不完整。它没有 BasicAWSCredentials 类。
2.)I've looked at the more at the sample but it is not straightforward as #1 and I couldn't get the core upload functionality with credentials.
2.)我已经在示例中查看了更多内容,但它并不像#1 那样简单,而且我无法使用凭据获得核心上传功能。
I appreciate your help
我感谢您的帮助
回答by Yangfan
Sorry the post was written a long time ago :).
对不起,这篇文章是很久以前写的:)。
1) To make it work, you need aws-android-sdk-core-2.2.5.jar in addition to aws.android-sdk-s3-2.2.5.jar.
1) 要使其工作,除了 aws.android-sdk-s3-2.2.5.jar 之外,您还需要 aws-android-sdk-core-2.2.5.jar。
2) Which sample are you referring to? Recently the AWS Android SDK introduced TransferUtility as an replacement of TransferManager. You can find a sample here. Also there is a blog which explains the migration AWS SDK for Android Transfer Manager to Transfer Utility Migration Guide.
2)你指的是哪个样本?最近,AWS Android SDK 引入了 TransferUtility 作为 TransferManager 的替代品。您可以在此处找到示例。还有一个博客解释了将AWS SDK for Android Transfer Manager 迁移到 Transfer Utility Migration Guide。
PS: it's not recommended to use AWSBasicCredentials in a mobile app. Instead, try Cognito Identity. See its developer guide.
PS:不建议在移动应用中使用 AWSBasicCredentials。相反,请尝试 Cognito Identity。请参阅其开发人员指南。
回答by Ignacio Gutierrez
S3UploadService is a library that handles uploads to Amazon S3. It provides a service called S3UploadService with a static method where you provide a Context (so the static method can start the service), a File, a boolean indicating if said file should be deleted after upload completion and optionally you can set a callback (Not like an ordinary callback though. The way this works is explained in the README file).
S3UploadService 是一个处理上传到 Amazon S3 的库。它提供了一个名为 S3UploadService 的服务,其中包含一个静态方法,您可以在其中提供一个 Context(因此该静态方法可以启动该服务)、一个 File、一个指示在上传完成后是否应删除所述文件的布尔值,您还可以选择设置回调(不就像一个普通的回调。它的工作方式在自述文件中解释)。
It's an IntentService so the upload will run even if the user kills the app while uploading (because its lifecycle is not attached to the app's lifecycle).
它是一个 IntentService,因此即使用户在上传时终止了应用程序,上传也会运行(因为它的生命周期不附加到应用程序的生命周期)。
To use this library you just have to declare the service in your manifest:
要使用此库,您只需在清单中声明服务:
<application
...>
...
<service
android:name="com.onecode.s3.service.S3UploadService"
android:exported="false" />
</application>
Then you build an S3BucketData instance and make a call to S3UploadService.upload():
然后构建一个 S3BucketData 实例并调用 S3UploadService.upload():
S3Credentials s3Credentials = new S3Credentials(accessKey, secretKey, sessionToken);
S3BucketData s3BucketData = new S3BucketData.Builder()
.setCredentials(s3Credentials)
.setBucket(bucket)
.setKey(key)
.setRegion(region)
.build();
S3UploadService.upload(getActivity(), s3BucketData, file, null);
To add this library you need to add the JitPack repo to your root build.gradle:
要添加此库,您需要将 JitPack 存储库添加到您的根 build.gradle:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
and then add the dependency:
然后添加依赖项:
dependencies {
compile 'com.github.OneCodeLabs:S3UploadService:1.0.0@aar'
}
Here is a link to the repo: https://github.com/OneCodeLabs/S3UploadService
这是存储库的链接:https: //github.com/OneCodeLabs/S3UploadService
Hope this helps
希望这可以帮助