Reputation: 1437
I was previously using AWSS3TransferManager to upload images to my s3 bucket and everything was working perfectly, but I decided that background uploads were necessary so decided to switch over to AWSS3TransferUtilityManager. I've implemented the code exactly as shown in the guide but the upload never starts:
Instantiate AWS Code (In app 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 temporary directory to save image to for upload:
//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")
}
}
Set up Transfer Utility Expression:
let expression:AWSS3TransferUtilityUploadExpression = AWSS3TransferUtilityUploadExpression()
Progress update code:
//-------------------------
//Progress Bar Update
expression.progressBlock = { (task: AWSS3TransferUtilityTask,progress: NSProgress) -> Void in
dispatch_async(dispatch_get_main_queue(),{
print("Image upload progress update: \(progress.fractionCompleted)")
})
}
Mark ACL as public
//Mark ACL as public
expression.setValue("public-read", forRequestParameter: "x-amz-acl")
expression.setValue("public-read", forRequestHeader: "x-amz-acl" )
Completion handler code:
//-------------------------
//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:
//--------------------------
//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
})
So my bucket is in US-West-1 and identity pool is in US-East-1 and I know there have been some posts about making sure the bucket and identity pool are in the same region but that should be taken care of with the following line which I've implemented:
let configuration = AWSServiceConfiguration(region: AWSRegionType.USWest1, credentialsProvider: credentialsProvider)
The behavior I get is that the completion handler and progress block code are never called. What am I missing here? Once again, uploads were/are working perfectly to the same bucket using the AWSS3TransferManager
Upvotes: 6
Views: 6601
Reputation: 169
Tried your code, seems to be working fine for me.
task.exception != nil
doesn't work though so just remove that.
Upvotes: 0
Reputation: 1437
Figured it out by delving through trial and error and some inspiration from the AWS forums. Apparently AWSS3TransferUtilityManager doesn't work with buckets that have special characters in their names (in my case a dash). Frustrating because this wasn't a problem with the AWSS3TransferManager.
Upvotes: 6