Search code examples
androidamazon-web-servicesamazon-s3aws-amplify

AWS Amplify. Use existing resources


I have followed the tutorial:

https://aws-amplify.github.io/docs/android/start?ref=amplify-android-btn

To integrate AWS S3 with my android application.

I'm able to download a file, and everything works fine.

I've notice that when i create a enviroment, it creates a new bucket, i don't want to create a new bucket, i want to use an existing one.

I've tried to change the bucket in the awsconfiguration.json. But it does not work.

"S3TransferUtility": {
    "Default": {
        "Bucket": "somebucket",
        "Region": "us-east-1"
    }
}
AmazonS3Exception: Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: <ID>)

I don't want to create a new bucket, i want to be able to access crated buckets that share objects across other apps.


Solution

  • I solved. For anyone who is interested in.

    For using an existing bucket, without creating a environment with amplify. The only thing i done was to add manually the file awsconfiguration.json inside the raw folder. I do not use amplify.

    So, my awsconfiguration.json looks like:

    {
    "Version": "1.0",
    "IdentityManager": {
        "Default": {}
    },
    "CredentialsProvider": {
        "CognitoIdentity": {
            "Default": {
                "PoolId": "cognito-pool-id",
                "Region": "region"
            }
        }
    },
    "S3TransferUtility": {
        "Default": {
            "Bucket": "somebucket",
            "Region": "region"
        }
    }
    }
    

    And to download objects from S3 i make my TransferUtility like this:

    val transferUtility = TransferUtility.builder()
            .context(context)
            .awsConfiguration(AWSMobileClient.getInstance().configuration)
            .s3Client(getAmazonS3Client())
            .build()
    
    private fun getAmazonS3Client(): AmazonS3Client{
        if (amazonS3Client == null) {
            amazonS3Client = AmazonS3Client(
                    CognitoCachingCredentialsProvider(
                        context, 
                        BuildConfig.S3_POOL_ID, 
                        Regions.US_EAST_2
                    ),
                    Region.getRegion(Regions.US_EAST_2)
                    )
        }
    
        return amazonS3Client!!
    }