尝试使用Cognito从S3下载文件

时间:2018-04-15 05:06:45

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

我使用AWS S3作为iOS应用中声音文件存储的后端。我可以根据需要将声音文件上传到存储桶,但是我无法使下载工作。

我首先希望使用PFFile让事情有效,但由于我没有按预期成功,我做了一些研究,并给人留下使用Cognito的方法。由于这是我第一次使用它,我可能会错过一些重要的部分。

这是应用程序:didFinishLaunchingWithOptions方法。

func application(_ application: UIApplication,
                 didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    window = UIWindow(frame: UIScreen.main.bounds)
    ..........

    // For the AWSS3 configuration:
    let poolID = "us-east-1:123.........",
    credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1,
                                                       identityPoolId: poolID)
    let awsConfig = AWSServiceConfiguration(region: .APNortheast1,
                                            credentialsProvider: credentialProvider)
    AWSServiceManager.default().defaultServiceConfiguration = awsConfig

    return true
}

在某些时候我执行了这段代码:

transferManager = AWSS3TransferManager.default()

要下载声音文件,请运行以下代码:

let soundFileName = ....,
downloadFileURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(soundFileName),
downloadRequest = AWSS3TransferManagerDownloadRequest()
downloadRequest?.bucket = "theappbucket"
downloadRequest?.key = soundFileName
downloadRequest?.downloadingFileURL = downloadFileURL

transferManager.download(downloadRequest!).continueWith(
    executor: AWSExecutor.mainThread(),
    block: {
        (task:AWSTask<AnyObject>) -> Any? in
        if let error = task.error {
            print("Error in \(#function)\n" + error.localizedDescription)
            print("Error in \(#function)\n\(error)")
            return nil
        }
        .....
    });

我收到此错误:

The operation couldn’t be completed. (com.amazonaws.AWSServiceErrorDomain error 11.)
UserInfo={HostId=ND....../FY=, Message=Access Denied, Code=AccessDenied, RequestId=EAC.....}

有些疑问,我也尝试使用.USEast1代替.APNortheast1,在这种情况下错误变为:

The operation couldn’t be completed. (com.amazonaws.AWSS3ErrorDomain error 0.)

但由于我的存储桶设置在Asia Pacific (Tokyo),我认为正确的设置为.APNortheast1

有人能指出我正在做的事情吗?

0 个答案:

没有答案