iosamazon-s3aws-mobilehubaws-sdk-iosawss3transferutility

swift iOS S3 access denied while using Mobile Hub configuration


I am currently using Mobile Hub in my app. When I tried to use S3 to upload photos to my bucket, I copied in verbatim the function from documentation here to download/upload files: https://docs.aws.amazon.com/aws-mobile/latest/developerguide/add-aws-mobile-user-data-storage.html

This is my code in Swift trying to utilize the S3TransferUtility:

func uploadData(data: Data, fileName: String) {

let expression = AWSS3TransferUtilityUploadExpression()
expression.progressBlock = {(task, progress) in
    DispatchQueue.main.async(execute: {
        // Do something e.g. Update a progress bar.
    })
}

var completionHandler: AWSS3TransferUtilityUploadCompletionHandlerBlock?
completionHandler = { (task, error) -> Void in
    DispatchQueue.main.async(execute: {
        // Do something e.g. Alert a user for transfer completion.
        // On failed uploads, `error` contains the error object.
    })
}

let transferUtility = AWSS3TransferUtility.default()

transferUtility.uploadData(data,
                           bucket: "my bucket name",
                           key: fileName,
                           contentType: "image/jpeg",
                           expression: expression,
                           completionHandler: completionHandler).continueWith {
                            (task) -> AnyObject? in
                            if let error = task.error {
                                print("Error: \(error.localizedDescription)")
                            }

                            if let res = task.result {
                                // Do something with uploadTask.
                                print(res)
                            }
                            return nil
}
}

I get this error in the console: Image of Error in Console

I've investigated into AWS S3 and the awsconfiguration.json file provided and everything seems to be in order:

AWS IAM Console

AWSConfiguration.json file in my project

Right now I'm confused because I thought that Mobile Hub was supposed to take care of the IAM configurations and what not for every thing.

Could someone please point me in the right direction to get this fixed? Thank you.


Solution

  • AWS Mobile Hub creates the following folders in your S3 bucket. Each folder has preconfigured permissions in IAM based on the Cognito authentication status of your app's user.

    This prior answer might also be helpful: How do I upload a file into a protected s3 bucket from Swift using the s3 bucket created by AWS mobile hub?