所有aws-sdk-ios示例都使用AWSS3TransferManager
异步上传数据 。
现在通常,这很好,但由于其他原因,我的工作已经在NSOperation
子类中,所以我想同步上传到S3以保持简单(否则,我' d需要实现一个异步NSOperation
,并且有更多的样板...)
任何人都知道怎么做?
答案 0 :(得分:7)
如果有人好奇,我想出来了:
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1,
identityPoolId: AwsCognitoIdentityPoolId)
let configuration = AWSServiceConfiguration(region: AWSRegionType.USEast1,
credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
let S3Client = AWSS3.defaultS3()
let putObjectRequest = AWSS3PutObjectRequest()
putObjectRequest.bucket = AwsBucketName
putObjectRequest.key = key
putObjectRequest.body = logFilePathURL
do {
let fileAttributes = try self.fileManager.attributesOfItemAtPath(logFilePathURL.path!)
let fileSizeNumber = fileAttributes[NSFileSize] as! NSNumber
putObjectRequest.contentLength = NSNumber(longLong: fileSizeNumber.longLongValue)
} catch _ as NSError {
// TODO handle error
}
S3Client.putObject(putObjectRequest).continueWithBlock { (task: AWSTask) -> AnyObject? in
return nil
}.waitUntilFinished()
...耶。我无法在任何地方找到的示例,因此我必须阅读源代码并弄清楚事情。关于那个aws-sdk-ios
库,谈论过度工程......需要一段时间才能通过所有这些间接层来解决它。
无论如何,在我return nil
处理错误的最后一个块中等等。