How to upload an image from my ios application to my bucket stored on aws s3 and do it all in Swift?

There are many, many examples of what is written in obj C, but I'm looking for a Swift solution. For now, all I could find was https://github.com/awslabs/aws-sdk-ios-samples/tree/master/S3TransferManager-Sample/Swift , but this is not so clear to me.

I already set up the s3 webpage on aws, I also created and populated the Constans.swift file:

 import AWSS3 import Foundation let CognitoRegionType = AWSRegionType.XXXXX let DefaultServiceRegionType = AWSRegionType.XXXXX let CognitoIdentityPoolId = "MyCognitoIdentityPoolId" let S3BucketName = "MyS3BucketName" 

I also added the following lines to AppDelegate.swift :

 let credentialsProvider = AWSCognitoCredentialsProvider(regionType: CognitoRegionType, identityPoolId: CognitoIdentityPoolId) let configuration = AWSServiceConfiguration(region: DefaultServiceRegionType, credentialsProvider: credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration 

I also have a class in Swift with a button and an image view controller, and while I press the button, I can take a photo from a gallery or camera, and it is displayed on the image. This is my code responsible for this:

 @IBOutlet weak var imageView: UIImageView! @IBAction func captureImage(sender: AnyObject) { let imageFromSource = UIImagePickerController() imageFromSource.delegate = self imageFromSource.allowsEditing = false if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){ imageFromSource.sourceType = UIImagePickerControllerSourceType.Camera } else{ imageFromSource.sourceType = UIImagePickerControllerSourceType.PhotoLibrary } self.presentViewController(imageFromSource, animated: true, completion: nil) } func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imageView.image = image self.dismissViewControllerAnimated(true, completion: {}) } 

Now all I want to do is add a new button that will be responsible for loading this photo into my s3 bucket, something like this tutorial: https://www.youtube.com/watch?v=WZ54fH8AFUk (unfortunately, in objective c here, I would be so grateful if you could help me with the quick version ...). Thanks!

==== EDIT

I am following a tutorial published by @the_pantless_coder (this https://www.codementor.io/tips/5748713276/how-to-upload-images-to-aws-s3-in-swift ) and I decided to modify the existing imagePickerController method , so it looks something like this:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { imageView.image = image self.dismissViewControllerAnimated(true, completion: {}) let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType, identityPoolId:CognitoIdentityPoolId) let configuration = AWSServiceConfiguration(region:CognitoRegionType, credentialsProvider:credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration let ext = "png" let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: ext)! let uploadRequest = AWSS3TransferManagerUploadRequest() uploadRequest.body = imageURL uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext uploadRequest.bucket = S3BucketName uploadRequest.contentType = "image/" + ext } 

but I have a problem with this line:

 let imageURL = NSBundle.mainBundle().URLForResource("image", withExtension: ext)! 

how can i get imageURL when i only have imageView.image = image ?

+6
source share
2 answers

Please make sure you have a bridge header in place where you can import the appropriate AWS headers. Here's an example here on GitHub.

Doing this should make S3 methods available.

-Rohan

+3
source

Based on input from @ the-pantless-coder, you can save the image to a temporary file and delete the file after the download is complete.

Try:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) { let path = NSTemporaryDirectory().stringByAppendingString("image.jpeg") if let data = UIImageJPEGRepresentation(image, 0.8) { data.writeToFile(path, atomically: true) } self.dismissViewControllerAnimated(true, completion: {}) let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType, identityPoolId:CognitoIdentityPoolId) let configuration = AWSServiceConfiguration(region:CognitoRegionType, credentialsProvider:credentialsProvider) AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration let ext = "jpeg" let uploadRequest = AWSS3TransferManagerUploadRequest() uploadRequest.body = path uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext uploadRequest.bucket = S3BucketName uploadRequest.contentType = "image/" + ext } 
+3
source

All Articles