[英]How to securely download images from read-only AWS S3 Bucket to an iOS app using AWS amplify?
[英]How can I allow guest Amplify users to download files from my S3 bucket using the AWS Amplify iOS SDK?
我已经使用 Amplify、CLI 控制台在 Amazon AWS 上设置了一个存储桶。 我遵循了 Amplify iOS SDK 的文档,并在 iOS 应用程序上安装了以下设置代码:
do {
try Amplify.add(plugin: AWSCognitoAuthPlugin())
try Amplify.add(plugin: AWSS3StoragePlugin())
try Amplify.configure()
} catch {
print(message: "An error occurred setting up Amplify: \(error)")
}
当我尝试下载资源时,出现以下错误:
StorageError: The HTTP response status code is [403].
我正在使用以下代码:
var url: URL = //url to download to
var key: String = //key for the file as specified in S3
operation = Amplify.Storage.downloadFile(key: key,
local: url) { [weak self] progress in
guard let _ = self else { return }
print(message: "progress: \(progress.fractionCompleted)")
} resultListener: { [weak self] result in
guard let _ = self else { return }
switch result {
case .success:
print(message: "success!")
case .failure(let error):
print(message: "error: \(error)")
}
}
我通过amplify configure
设置的用户附加了AdministratorAccess
和AmazonS3ReadOnlyAccess
策略:
我错过了什么?
通过amplify configure
创建的用户由 Amplify 用于资源配置 - 您已将AdministratorAccess
和AmazonS3ReadOnlyAccess
策略分配给您的 Amplify 用户,但未分配给未经身份验证的用户角色,这是 Amplify 访客用户使用的角色。
首先找到未经身份验证的角色的角色名称,登录Amazon Cognito 控制台,选择Manage Identity Pools ,然后选择 Amplify 创建的身份池。
单击页面右上角的编辑身份池,您将看到一个类似于下图的页面,其中有一个标记为Unauthenticated role的下拉框:
用于访客/未经身份验证的用户的当前角色将在下拉列表中。
在 IAM 控制台中查找角色名称,然后将 AWS 托管的AmazonS3ReadOnlyAccess
策略附加到角色。
您的未经身份验证的用户现在应该有权从 S3 存储桶读取文件,并且您不应再收到403 Access Denied
错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.