首页 > 解决方案 > Amplify 无法从 s3 下载对象

问题描述

我正在尝试使用 amplify-swift 将现有 s3 存储桶中的对象下载到 IOS 应用程序。我已经按照教程初始化放大和导入存储。我正在尝试调用未经身份验证的用户-> 在 Cognito 控制台中,我为 unauth 用户定义了一个角色(并允许访问 unauth 用户),授予他对我的存储桶的权限并使用模拟器验证 getObject 是否有效。调用此代码时:

 let key = "images/some_file.txt"
        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)")
        }
    
        
        
       Amplify.Storage.downloadData(
            key: key,
            progressListener: { progress in
                print("Progress: \(progress)")
            }, resultListener: { (event) in
                switch event {
                case let .success(data):
                    print("Completed: \(data)")
                case let .failure(storageError):
                    print("Failed: \(storageError.errorDescription). \(storageError.recoverySuggestion)")
            }
        })

我收到此错误:

Amplify 配置了存储插件 2021-03-31 18:09:09.988304+0300 ota[30840:728969] [] nw_protocol_get_quic_image_block_invoke dlopen libquic failed Progress: <NSProgress: 0x6000001d50e0> : Parent: 0x0 (portion: 0) / Fraction completed: /已完成:-1
的 243 失败:HTTP 响应状态代码为 [403].. TODO 一些状态代码以恢复消息映射器。有关 HTTP 状态代码的更多信息,请查看https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

我检查了池 ID、存储桶名称是否在 json 文件中都正确定义。我错过了什么?

编辑:

这似乎可行,所以我知道实际问题不在于权限,而在于放大默认下载:

let expression = AWSS3TransferUtilityDownloadExpression()
            expression.progressBlock = {(task, progress) in DispatchQueue.main.async(execute: {
                print("Downloading...")
            })
            }
        let credentialProvider = AWSCognitoCredentialsProvider(regionType: .EUWest1, identityPoolId: "eu-west-1:xxxxxx")
        let configuration = AWSServiceConfiguration(region: .EUWest1, credentialsProvider: credentialProvider)
        AWSS3TransferUtility.register(with: configuration!, forKey: "customTU")
        let tu = AWSS3TransferUtility.s3TransferUtility(forKey: "customTU")
        tu?.downloadData(fromBucket: bucket, key: _key, expression: expression) { (task, URL, data, error) in
                    if error != nil {
                        print(error!)
                    }
                    DispatchQueue.main.async(execute: {
                        print("Got here")
                        let rawJSON = (String(data: data!, encoding: .utf8))!
                        print(rawJSON)
                    })
            }

标签: iosswiftamazon-s3amplify

解决方案


推荐阅读