从 IOS 上的 S3 存储桶下载时出错。

Error downloading from S3 bucket on IOS.

我很难尝试下载已上传到我的 S3 存储桶中的特定文件。

我创建了一个名为 "Photos" 的存储桶并上传了名为 "test.png"

的文件

在我的 AppDelegate 中设置我的 CredentialProvider 后,我尝试使用以下代码下载该文件:

    let transferManager = AWSS3TransferManager.defaultS3TransferManager()

    let downloadingFilePath = NSTemporaryDirectory().stringByAppendingString("test.png")
    let downloadingFileUrl = NSURL(fileURLWithPath: downloadingFilePath)
    let downloadRequest = AWSS3TransferManagerDownloadRequest()
    downloadRequest.bucket = "photos"
    downloadRequest.key = "test.png"
    downloadRequest.downloadingFileURL = downloadingFileUrl
    transferManager.download(downloadRequest).continueWithExecutor(AWSExecutor.mainThreadExecutor(), withBlock: { (AWSTask) -> AnyObject! in

        //Handle errors
        if AWSTask.error != nil
        {

            print("Error downloading: \(AWSTask.error)")

            // Retrive information important for later downloading
        }
        else
        {
            print("Download succesful..")
            var uploadResult: AnyObject! = AWSTask.result
            print("Upload result: \(uploadResult)")

            let downloadOutput = AWSTask.result as! AWSS3TransferManagerDownloadOutput                

        }
        return nil

    })

但它一直给我错误:"The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint"

我尝试将 downloaderRequest.bucket 更改为我在存储桶属性中找到的端点。 downloaderRequest.bucket = "photos.s3-website-sa-east-1.amazonaws.com"

但现在它说 "The specified bucket does not exist, Code=NoSuchBucket"

知道我做错了什么吗?谢谢。

下载之前,请设置所有与访问AWS服务相关的必需信息,请先在您的应用程序委托中应用以下代码,

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    let credentialsProvider = AWSStaticCredentialsProvider(accessKey: YOUR_AWS_ACCESS_KEY, secretKey: YOUR_AWS_SECRET_KEY)
    let defaultServiceConfiguration = AWSServiceConfiguration(region: AWSRegionType.SAEast1, credentialsProvider: credentialsProvider) //Your region endpoint
    AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = defaultServiceConfiguration

    AWSLogger.defaultLogger().logLevel = .Verbose
    return true
}

注意:请注意区分大小写的存储桶名称拼写。

 func downloadFile()
 {
            let transferManager = AWSS3TransferManager.defaultS3TransferManager()
            let downloadingFilePath = NSTemporaryDirectory().stringByAppendingString("test.png")
            let downloadingFileURL = NSURL(fileURLWithPath: downloadingFilePath as String )

            let downloadReadRequest : AWSS3TransferManagerDownloadRequest = AWSS3TransferManagerDownloadRequest()
            downloadReadRequest.bucket = "photos"
            downloadReadRequest.key =  "test.png"
            downloadReadRequest.downloadingFileURL = downloadingFileURL

            let task = transferManager.download(downloadReadRequest)
            task.continueWithBlock { (task) -> AnyObject? in

                if task.error != nil
                {
                    // Success
                }
                else
                {
                    // Error
                }  
             return nil
            }
}

我终于明白了。 所以这里的问题是我在南美地区创建了我的存储桶,而亚马逊找不到它。 甚至当我点击我的亚马逊区域时它告诉我 "S3 does not require region selection." 你需要在 "US Standard".

中创建它

出现此问题是因为 s3 存储桶的区域与我们在配置 s3 时使用的区域不匹配 这是代码:

let credentialsProvider = AWSCognitoCredentialsProvider(regionType: cognitoRegion,
                                                                identityPoolId: cognitoIdentityPoolId,
                                                                identityProviderManager: customIdentityProvider)

所以,这里的区域类型必须与您的存储桶区域类型相同。前任。

如果这是您的存储桶,那么您在代码中的区域类型必须是:AWSRegionType.APSouth1