Simple upload to S3 from android

帅比萌擦擦* 提交于 2019-12-06 08:33:20

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.

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.

PS: it's not recommended to use AWSBasicCredentials in a mobile app. Instead, try Cognito Identity. See its developer guide.

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).

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).

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():

    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:

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

Hope this helps

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!