如何使用 AWS amplify 将图像从只读 AWS S3 存储桶安全地下载到 iOS 应用程序?

How to securely download images from read-only AWS S3 Bucket to an iOS app using AWS amplify?

我正在关注来自 aws 的官方 tutorial 从 S3 存储桶下载图像。 在 IAM 控制台中,我创建了一个对存储桶具有只读访问权限的新用户。 在 .aws 文件夹中添加凭据并使用 amplify CLI 后,创建了新的 Cognito 用户池、Cognito 身份池和 S3 存储桶。

我已经在 Swift 应用程序中安装了 Amplify、AmplifyPlugins/AWSS3StoragePlugin、AmplifyPlugins/AWSCognitoAuthPlugin pods。 在调用放大推送后,amplifyconfiguration.json 和 awsconfiguration.json 已使用凭据更新。

在 AppDelegate 中我设置了以下演示代码:

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)")
        }
    })
}

问题是我接收失败:

authError:

0: 字符串“没有用户登录以检索身份 ID”
1:字符串“调用 Auth.signIn 以登录用户或在 AWS Cognito 身份池中启用未经身份验证的访问”

使用 AWS 最新的 amplify SDK 并使用只读权限在 S3 上安全连接的最佳步骤是什么?理想情况下,我想使用我创建的现有存储桶而不是生成的存储桶。

您确定在 Cognito 设置阶段允许访客访问吗?

? Who should have access:
    `Auth and guest users`
? What kind of access do you want for Authenticated users?
    `create/update, read, delete`
? What kind of access do you want for Guest users?
    `create/update, read, delete`

有关详细信息,请参阅 https://docs.amplify.aws/lib/auth/guest_access/q/platform/ios。您要么需要以具体身份登录,要么具有“访客访问”身份。

关于您使用现有 S3 存储桶的问题,请参阅 https://docs.amplify.aws/lib/storage/existing-resources/q/platform/ios

  1. Bucket with Write Access:如果你想将数据或文件上传到你在 你的例子那么你需要提供一些写访问权限 role/user。您不能上传超过 read-only 个存储桶的任何内容。
  2. 未验证:您还可以启用对 使用 AWS Cognito 的存储桶。有关详细信息,您可以查看此 Whosebug 问题.
  3. withAuthenticator: 除了通过未经身份验证的用户使用,您还可以 认证 使用 withAuthenticator component.
  4. Pre-signed URL: A pre-signed URL 允许你授予临时 访问无权直接访问 运行 AWS 的用户 您帐户中的操作。 pre-signed URL 已与您签署 凭据,任何用户都可以使用。想要查询更多的信息,