This is the Swift code that is used to upload images. It will not save the image, it will just add it to the array that I declared in viewDidLoad ()
func downloadImage(key: String){
var completionHandler: AWSS3TransferUtilityDownloadCompletionHandlerBlock?
let S3BucketName: String = "your_s3_bucketName"
let S3DownloadKeyName: String = key
let expression = AWSS3TransferUtilityDownloadExpression()
expression.downloadProgress = {(task: AWSS3TransferUtilityTask, bytesSent: Int64, totalBytesSent: Int64, totalBytesExpectedToSend: Int64) in
dispatch_async(dispatch_get_main_queue(), {
let progress = Float(totalBytesSent) / Float(totalBytesExpectedToSend)
NSLog("Progress is: %f",progress)
})
}
completionHandler = { (task, location, data, error) -> Void in
dispatch_async(dispatch_get_main_queue(), {
if ((error) != nil){
NSLog("Failed with error")
NSLog("Error: %@",error!);
}
else{
self.collectionImages[S3DownloadKeyName] = UIImage(data: data!)
self.colView.reloadData()
}
})
}
let transferUtility = AWSS3TransferUtility.defaultS3TransferUtility()
transferUtility.downloadToURL(nil, bucket: S3BucketName, key: S3DownloadKeyName, expression: expression, completionHander: completionHandler).continueWithBlock { (task) -> AnyObject! in
if let error = task.error {
NSLog("Error: %@",error.localizedDescription);
}
if let exception = task.exception {
NSLog("Exception: %@",exception.description);
}
if let _ = task.result {
}
return nil;
}
}
source
share