如何设置Info.Plist以使AWSS3TransferManager.default工作

时间:2017-10-27 06:55:10

标签: swift

我在尝试从AWSS3TransferManager获取默认值时遇到错误。它说

'NSInternalInconsistencyException',原因:'服务配置为nil。在使用此方法之前,您需要配置Info.plist或设置defaultServiceConfiguration

这是我的代码

func downloadFromS3() {

    var downloadCount = 0
    for index in 0...2 {
        let  downloadingFilePath = NSTemporaryDirectory().appendingFormat("banner%d.jpg", index+1)
        let downloadingFileURL = NSURL.fileURL(withPath: downloadingFilePath)

        let transferManager = AWSS3TransferManager.default()
        let downloadRequest = AWSS3TransferManagerDownloadRequest()

        downloadRequest?.bucket = BPAWSBannerBucketName
        downloadRequest?.key = String(format: "banner%d.jpg",index+1)
        downloadRequest?.downloadingFileURL = downloadingFileURL

        print("started download process")

        let task = transferManager.download(downloadRequest!)
        task.continueWith (block: {
            (task) -> AnyObject! in

            if task.error != nil {
                print("Error: \(String(describing: task.error))")
            } else {
                print("Download successful")

                let image = UIImage(contentsOfFile: downloadingFilePath)
                self.bannerImageArr?.add(image!)
                downloadCount += 1
                if downloadCount == 3 {
                    DispatchQueue.main.async {
                        self.bannerCollectionView.reloadData()

                    }
                }
            }
            return nil
        }
        )
    }
}

1 个答案:

答案 0 :(得分:0)

您需要在application:didFinishLaunchingWithOptions:应用程序委托方法中设置defaultServiceConfiguration。

let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "YourIdentityPoolId")
let configuration = AWSServiceConfiguration(region: .USEast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration