从 android 简单上传到 S3

Simple upload to S3 from android

我在网上搜索了将简单文件从 android 上传到 s3 的方法,但找不到任何有效的方法,我认为这是因为缺少具体步骤。

1.) https://mobile.awsblog.com/post/Tx1V588RKX5XPQB/TransferManager-for-Android

这是我找到的最好的指南,但它没有说明要包含哪些库。我下载了 android 的 aws sdk,里面有很多罐子。所以凭直觉,我包括了 aws-android-sdk-s3-2.2.5.jar 但这并不完整。它没有 class BasicAWSCredentials。

2.)我查看了更多示例,但它并不像 #1 那样简单,而且我无法使用凭据获得核心上传功能。

感谢您的帮助

抱歉 post 是很久以前写的:)。

1) 要使其工作,除了 aws.android-sdk-s3-2.2.[= 之外,您还需要 aws-android-sdk-core-2.2.5.jar 20=].

2) 你指的是哪个样本?最近 AWS Android SDK 引入了 TransferUtility 作为 TransferManager 的替代品。您可以找到示例 here. Also there is a blog which explains the migration AWS SDK for Android Transfer Manager to Transfer Utility Migration Guide.

PS:不建议在移动应用程序中使用 AWSBasicCredentials。相反,试试 Cognito Identity。见其developer guide.

S3UploadService 是一个处理上传到 Amazon S3 的库。它提供了一个名为 S3UploadService 的服务,它带有一个静态方法,您可以在其中提供一个上下文(因此静态方法可以启动服务)、一个文件、一个布尔值,指示上传完成后是否应删除该文件,并且您可以选择设置回调(不是不过就像一个普通的回调。它的工作方式在 README 文件中有解释。

它是一个 IntentService,因此即使用户在上传时终止应用程序,上传也会 运行(因为它的生命周期不附加到应用程序的生命周期)。

要使用此库,您只需在清单中声明该服务:

<application
    ...>

    ...

    <service
        android:name="com.onecode.s3.service.S3UploadService"
        android:exported="false" />
</application>

然后构建一个 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);

要添加此库,您需要将 JitPack 存储库添加到您的根 build.gradle:

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

然后添加依赖:

dependencies {
    compile 'com.github.OneCodeLabs:S3UploadService:1.0.0@aar'
}

这是回购的 link: https://github.com/OneCodeLabs/S3UploadService

希望对您有所帮助