ios - Swift - AWS S3 Upload Image from Photo Library and download it -
i've looked many amazon docs didn't find enough information upload , download images s3 using swift.
how can that?
after doing many research i've got working,
import awss3 import awscore
upload:
i assume have implemented uiimagepickercontrollerdelegate
already.
step 1:
create variable holding url:
var imageurl = nsurl()
create upload completion handler obj:
var uploadcompletionhandler: awss3transferutilityuploadcompletionhandlerblock?
step 2: image url imagepickercontroller(_:didfinishpickingmediawithinfo:)
:
set value of
imageurl
in delegate method:func imagepickercontroller(picker: uiimagepickercontroller, didfinishpickingmediawithinfo info: [string : anyobject]){ //getting details of image let uploadfileurl = info[uiimagepickercontrollerreferenceurl] as! nsurl let imagename = uploadfileurl.lastpathcomponent let documentdirectory = nssearchpathfordirectoriesindomains(.documentdirectory, .userdomainmask, true).first! string // getting local path let localpath = (documentdirectory nsstring).stringbyappendingpathcomponent(imagename!) //getting actual image let image = info[uiimagepickercontrolleroriginalimage] as! uiimage let data = uiimagepngrepresentation(image) data!.writetofile(localpath, atomically: true) let imagedata = nsdata(contentsoffile: localpath)! imageurl = nsurl(fileurlwithpath: localpath) picker.dismissviewcontrolleranimated(true, completion: nil) }
step 3: call uploadimage
method after imageurl
set upload image bucket:
func uploadimage(){ //defining bucket , upload file name let s3bucketname: string = "bucketname" let s3uploadkeyname: string = "public/testimage.jpg" let expression = awss3transferutilityuploadexpression() expression.uploadprogress = {(task: awss3transferutilitytask, bytessent: int64, totalbytessent: int64, totalbytesexpectedtosend: int64) in dispatch_async(dispatch_get_main_queue(), { let progress = float(totalbytessent) / float(totalbytesexpectedtosend) print("progress is: \(progress)") }) } self.uploadcompletionhandler = { (task, error) -> void in dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){ print("failed error") print("error: \(error!)"); } else{ print("sucess") } }) } let transferutility = awss3transferutility.defaults3transferutility() transferutility.uploadfile(imageurl, bucket: s3bucketname, key: s3uploadkeyname, contenttype: "image/jpeg", expression: expression, completionhander: uploadcompletionhandler).continuewithblock { (task) -> anyobject! in if let error = task.error { print("error: \(error.localizeddescription)") } if let exception = task.exception { print("exception: \(exception.description)") } if let _ = task.result { print("upload starting!") } return nil; } }
download:
func downloadimage(){ var completionhandler: awss3transferutilitydownloadcompletionhandlerblock? let s3bucketname: string = "bucketname" let s3downloadkeyname: string = "public/testimage.jpg" 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) print("progress is: \(progress)") }) } completionhandler = { (task, location, data, error) -> void in dispatch_async(dispatch_get_main_queue(), { if ((error) != nil){ print("failed error") print("error: \(error!)") } else{ //set image var downloadedimage = uiimage(data: data!) } }) } let transferutility = awss3transferutility.defaults3transferutility() transferutility.downloadtourl(nil, bucket: s3bucketname, key: s3downloadkeyname, expression: expression, completionhander: completionhandler).continuewithblock { (task) -> anyobject! in if let error = task.error { print("error: \(error.localizeddescription)") } if let exception = task.exception { print("exception: \(exception.description)") } if let _ = task.result { print("download starting!") } return nil; } }
many jzorz
I really liked your blog post.Much thanks again. Awesome.
ReplyDeletepython online training
python training