Vlad Crow
Vlad Crow

Reputation: 39

Can't upload s3 bucket from android using cognito

Hi guys so I try to upload a picture from my emulator on a s3 bucket using android and cognito:

package com.example.s3_aws;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;

import com.amazonaws.auth.CognitoCachingCredentialsProvider;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferListener;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferState;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;

import java.io.File;

public class MainActivity extends AppCompatActivity {
    AmazonS3 s3;
    TransferUtility transferUtility;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        credentialsProvider();
        setTransferUtility();
        upload();

    }

    public void credentialsProvider(){
        // Initialize the Amazon Cognito credentials provider
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                getApplicationContext(),
                "idafa", // I actually have here the correct identity pool id
                Regions.US_EAST_1// Region
        );

        setAmazonS3Client(credentialsProvider);
    }

    public void setAmazonS3Client(CognitoCachingCredentialsProvider credentialsProvider){

        s3 = new AmazonS3Client(credentialsProvider);
        s3.setRegion(Region.getRegion(Regions.US_EAST_1));
    }

    public void setTransferUtility(){
        transferUtility = new TransferUtility(s3, getApplicationContext());
    }

    public void upload(){
        @SuppressLint("SdCardPath") final TransferObserver observer = transferUtility.upload(
                "s3.android.test",
                "test.png",
                new File("/sdcard/DCIM/Camera/IMG_20200415_001815.jpg"),
                CannedAccessControlList.PublicRead
        );

        observer.setTransferListener(new TransferListener() {
            @Override
            public void onStateChanged(int id, TransferState state) {
                if (state.equals(TransferState.COMPLETED)) {
                    Log.i("state changed", state+"");
                } else if (state.equals(TransferState.FAILED)) {
                    Log.e("state changed", state+"");
                }

            }
            @Override
            public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {

            }
            @Override
            public void onError(int id, Exception ex) {

            }
        });
    }


}

The problem is that it gives me some errors while I run the program:

Error occurred while accessing AndroidKeyStore to retrieve the key for keyAlias: com.amazonaws.android.auth.aesKeyStoreAlias

Error in retrieving the decryption key used to decrypt the data from the persistent store. Returning null for the requested dataKey

I'm leaving here also the inline policy i gave to the unauth role, maybe could be the problem here:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Statement0",
            "Effect": "Allow",
            "Action": [
                "s3:*"
            ],
            "Resource": "arn:aws:s3:::s3.android.test/*"
        }
    ]
}

Upvotes: 1

Views: 1001

Answers (1)

The Billionaire Guy
The Billionaire Guy

Reputation: 3552

this also happend to me here is what worked

add

-keep class com.amazonaws.mobileconnectors.s3.transferutility.TransferNetworkConnectionType {*;}

to proguard-rules.pro file in your project that solved my problem.

and here are my s3 dependencies

//amazon s3 bucket
implementation 'com.amazonaws:aws-android-sdk-core:2.15.1'
implementation 'com.amazonaws:aws-android-sdk-cognito:2.6.23'
implementation 'com.amazonaws:aws-android-sdk-s3:2.15.1'
//implementation 'com.amazonaws:aws-java-sdk:1.11.404'
implementation 'com.amazonaws:aws-android-sdk-ddb:2.2.0'
implementation ('com.amazonaws:aws-android-sdk-mobile-client:2.16.8') { transitive = true; }

saw it here

Upvotes: 2

Related Questions