【问题标题】:How to securely download images from read-only AWS S3 Bucket to an iOS app using AWS amplify?如何使用 AWS amplify 将图像从只读 AWS S3 Bucket 安全地下载到 iOS 应用程序?
【发布时间】:2021-01-10 23:09:02
【问题描述】:

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

我在 Swift 应用程序中安装了 Amplify、AmplifyPlugins/AWSS3StoragePlugin、AmplifyPlugins/AWSCognitoAuthPlugin pod。 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)")
        }
    })
}

问题是我收到失败:

身份验证错误:

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

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

【问题讨论】:

    标签: ios swift amazon-web-services amazon-s3 aws-amplify


    【解决方案1】:

    您是否确保在 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

    【讨论】:

      【解决方案2】:
      1. 具有写入权限的存储桶:如果您想将数据或文件上传到 S3 存储桶,如您在 你的例子那么你将需要提供对某些的写访问权限 角色/用户。您无法通过只读存储分区上传任何内容。
      2. 未经身份验证:您还可以启用未经身份验证的访问 使用 AWS Cognito 的存储桶。欲了解更多信息,您可以查看此Stackoverflow Question
      3. withAuthenticator:除了通过未经身份验证的用户使用,您还可以 认证 使用withAuthenticatorcomponent
      4. 预签名 URL:预签名 URL 允许您临时授予 访问无权直接运行 AWS 的用户 您帐户中的操作。预签名 URL 已使用您的签名 凭据,任何用户都可以使用。了解更多信息,

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-02-24
        • 2017-05-15
        • 2015-09-16
        • 2020-11-08
        • 1970-01-01
        • 2015-07-24
        • 1970-01-01
        • 2021-04-11
        相关资源
        最近更新 更多