Home / IOS Development / fast – How do I securely download photos from read-only AWS S3 Bucket to an iOS app using AWS amplify?

fast – How do I securely download photos from read-only AWS S3 Bucket to an iOS app using AWS amplify?



I follow the official tutorial from aws to download photos from an S3 Bucket. In the IAM console I have created a new user who only has read access to the bucket. After adding the credentials to the .aws folder and using the amplify CLI, Cognito User Pool, Cognito Identity Pool and S3 drawers were created.

I have installed Amplify, AmplifyPlugins / AWSS3StoragePlugin, AmplifyPlugins / AWSCognitoAuthPlugin pods in the Swift app. Amplifyconfiguration.json and awsconfiguration.json have been updated with credentials after calling amplify push.

In AppDelegate I have set the following demo code:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
    do {
        try Amplify.add(plugin: AWSCognitoAuthPlugin())
        try Amplify.add(plugin: AWSS3StoragePlugin())
        try Amplify.configure()
        print("Amplify configured with storage plugin")
    } catch {
        print("Failed to initialize Amplify with (error)")
    }
    
    self.testUploadData()
    
    return true
}

func testUploadData() {
    let dataString = "Example file contents"
    let data = dataString.data(using: .utf8)!
    Amplify.Storage.uploadData(key: "ExampleKey", data: data,
        progressListener: { progress in
            print("Progress: (progress)")
        }, resultListener: { (event) in
            switch event {
            case .success(let data):
                print("Completed: (data)")
            case .failure(let storageError):
                print("Failed: (storageError.errorDescription). (storageError.recoverySuggestion)")
        }
    })
}

The problem is that I receive the error:

authError:

0: String “No user logged in to recover identity ID”

;
1: string “Call Auth.signIn to log in to a user or enable unauthorized access in the AWS Cognito Identity Pool”

What are the best steps to use the latest amplified SDK from AWS and also secure connection on S3 with read-only permissions? Ideally, I would use and existing bucket I have created instead of the generated one.


Source link