Simple upload to S3 from android

17,794

Solution 1

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.

Solution 2

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

Share:
17,794
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    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.

    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.

    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.

    I appreciate your help