Reputation: 5839
There are lots and lots examples of that written in obj C, but I'm looking for a Swift
solution. So far all I could find is this one https://github.com/awslabs/aws-sdk-ios-samples/tree/master/S3TransferManager-Sample/Swift but it's not that clear for me.
I already configured the s3
on aws webpage, I also created and filled file Constans.swift
:
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 so far when I click the button I can take the photo from gallery or camera and it shows on the image view. This is my code responsible for that:
@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 is to add a new button that will be responsible for uploading this photo to my s3 bucket, something like in this tutorial: https://www.youtube.com/watch?v=WZ54fH8AFUk (unfortunately it's in objective c here, I would be so grateful if you could help me with a swift version...). Thanks!
====EDIT
I've been following a tutorial posted by @the_pantless_coder (this one https://www.codementor.io/tips/5748713276/how-to-upload-images-to-aws-s3-in-swift ) and I've decided to modify my existing method imagePickerController
, so far it looks 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 the imageURL when I only have a imageView.image = image
here?
Upvotes: 6
Views: 1680
Reputation: 2897
Based on input from @the-pantless-coder, you can save the image into temporary file and delete the file once the upload 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
}
Upvotes: 3
Reputation: 847
Please make sure you have a bridging header in place in place where you can import the appropriate AWS headers. There is an example located here on GitHub.
Doing this should make the S3 methods available.
-Rohan
Upvotes: 3