Search code examples
amazon-web-servicesamazon-s3awss3transferutility

Getting access denied error while uploading file to amazon s3 using tranferutility


Getting the following error while running the application in logcat

D/com.amazonaws.request: Received error response: com.amazonaws.services.s3.model.AmazonS3Exception: Access Denied (Service: null; Status Code: 403; Error Code: AccessDenied; Request ID: ZFW34X6QRZEBNR23), S3 Extended Request ID: cCcs8EOdirUSRRd9MyoUcMWDTjEvFLN/cqSYbAT3NCLR/hDSjxI/ATNX63Y9cbyDNABi8MnTauY=

Without using tranfer utility code works fine. When using transfer utility error occurs. Kindly find the code here

            getApplicationContext().startService(new Intent(getApplicationContext(), TransferService.class));
    AWSMobileClient.getInstance().initialize(getApplicationContext(), new Callback<UserStateDetails>() {
        @Override
        public void onResult(UserStateDetails result) {
            Log.i("AWSMobile Clinet","initialised awsmobile client");
            TransferUtility transferUtility=TransferUtility.builder().context(getApplicationContext())
                    .awsConfiguration(AWSMobileClient.getInstance().getConfiguration())
                    .s3Client(new AmazonS3Client(AWSMobileClient.getInstance()))
                    .build();
            try {
                File exampleFile = new File("/data/data/com.dieto.app/databases/user.db");
                TransferObserver uploadObserver=transferUtility.upload( "dietodata144832- 
             dev",number+".db",exampleFile);

            }
            catch (Exception e){
                Log.e("File load","File load failed"+e.toString());
            }

Solution

  • Check if the user has access to the that specific S3 bucket or not. You can do this by navigating to IAM on AWS console, check the policies attached to your user and check if the user has AWSS3FullAccess policy attached or not. See the policy attached.

    After you attach this policy, you shouldn't get access denied error.