我以前使用过AWSS3TransferManager将图像上传到s3存储桶,并且一切运行正常,但是我认为必须进行后台上传,因此决定切换到AWSS3TransferUtilityManager。我已经完全按照指南中的说明实现了代码,但是上传从未开始:
实例化AWS Code(在应用程序委托(delegate)中-didFinishLaunching ...):
//AWS SDK
let credentialsProvider = AWSCognitoCredentialsProvider(regionType: AWSRegionType.USEast1, identityPoolId: "my_pool_id")
let configuration = AWSServiceConfiguration(region: AWSRegionType.USWest1, credentialsProvider: credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
设置临时目录以将图像保存到上载:
//Set up and potentially clear temporary image directory before uploading
let path = (NSTemporaryDirectory() as NSString).stringByAppendingPathComponent("image.png")
let thumbnailUrl = NSURL(fileURLWithPath: path)
var error: NSError? = nil
if NSFileManager.defaultManager().fileExistsAtPath(thumbnailUrl.path!) {
do {
try NSFileManager.defaultManager().removeItemAtPath(thumbnailUrl.path!)
print("Successfully cleared temporary directory")
} catch let fileError as NSError {
error = fileError
print("Error clearing temporary image directory")
}
}
设置传输实用程序表达式:
let expression:AWSS3TransferUtilityUploadExpression = AWSS3TransferUtilityUploadExpression()
进度更新代码:
//-------------------------
//Progress Bar Update
expression.progressBlock = { (task: AWSS3TransferUtilityTask,progress: NSProgress) -> Void in
dispatch_async(dispatch_get_main_queue(),{
print("Image upload progress update: \(progress.fractionCompleted)")
})
}
将ACL标记为公开
//Mark ACL as public
expression.setValue("public-read", forRequestParameter: "x-amz-acl")
expression.setValue("public-read", forRequestHeader: "x-amz-acl" )
完成处理程序代码:
//-------------------------
//Completion handler
self.imageUploadCompletionHandler = { (task:AWSS3TransferUtilityUploadTask, error:NSError?) -> Void in
print("Image upload complete")
dispatch_async(dispatch_get_main_queue(), {
if(error != nil){
print("Failure uploading thumbnail")
}else{
print("Success uploading thumbnail")
}
})
}
上载:
//--------------------------
//Upload Thumbnail
AWSS3TransferUtility.defaultS3TransferUtility().uploadFile(thumbnailUrl, bucket: "exampleBucket", key: "exampleKey", contentType: "image/jpeg", expression: expression, completionHander: self.imageUploadCompletionHandler).continueWithBlock({ (task:AWSTask) -> AnyObject? in
if(task.error != nil){
print("Error uploading thumbnail: \(task.error)")
s3RequestSuccessful = false
}
if(task.exception != nil){
print("Exception uploading thumbnail: \(task.exception)")
s3RequestSuccessful = false
}
if(task.result != nil){
print("Starting upload...")
}
return nil
})
因此,我的存储桶位于US-West-1,身份存储池位于US-East-1,我知道有一些关于确保存储桶和身份存储池位于同一区域的帖子,但是应该注意我已经实现的以下行:
let configuration = AWSServiceConfiguration(region: AWSRegionType.USWest1, credentialsProvider: credentialsProvider)
我得到的行为是从未调用完成处理程序和进度块代码。我在这里想念什么?再一次,使用AWSS3TransferManager将上传完美地/成功地添加到同一存储桶中
最佳答案
通过仔细研究和尝试错误以及从AWS论坛中获得一些启发来解决问题。显然,AWSS3TransferUtilityManager不适用于名称中带有特殊字符的存储桶(在我的情况下为破折号)。令人沮丧的是,这不是AWSS3TransferManager的问题。