Upload Image to AWS S3 Bucket. In Swift.
In this blog post I am going to share with you how to upload an image to Amazon AWS S3 Bucket. I have split information into smaller videos and it will cover:
http://swiftdeveloperblog.com/image-upload-example/
http://swiftdeveloperblog.com/upload-image-to-aws-s3-bucket-in-swift/
- Create AWS S3 Bucket
- Edit S3 Bucket policy to allow users read files from it
- Use AWS Cognito to create a Federated Identity Pool
- Install CocoaPods and use it to download AWS SDK for iOS
- Write Swift code to upload image to AWS S3 Bucket
Introduction
Create AWS S3 Bucket
AWS Bucket Policy
- {
- "Version": "2008-10-17",
- "Statement": [
- {
- "Sid": "AddPerm",
- "Effect": "Allow",
- "Principal": "*",
- "Action": "s3:GetObject",
- "Resource": "arn:aws:s3:::learn-swift/*"
- }
- ]
- }
Create Federated Identity Pool with AWS Cognito
Download and Setup AWS SDK for iOS Platform
Write Code to Upload Image to AWS S3 Bucket
Select image with UIImagePickerController and upload it to AWS S3 bucket
Upload Image to AWS S3 Bucket. Source code in Swift
- import UIKit
- import AWSCore
- import AWSS3
- import Photos
- class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
- var showImagePickerButton: UIButton!
- var myImageView: UIImageView!
- var selectedImageUrl: NSURL!
- var myActivityIndicator: UIActivityIndicatorView!
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view, typically from a nib.
- setupImagePickerButton()
- setupImageView()
- setupUploadButton()
- setUpActivityIndicator()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- override func viewWillAppear(animated: Bool) {
- super.viewWillAppear(animated)
- }
- func startUploadingImage()
- {
- var localFileName:String?
- if let imageToUploadUrl = selectedImageUrl
- {
- let phResult = PHAsset.fetchAssetsWithALAssetURLs([imageToUploadUrl], options: nil)
- localFileName = phResult.firstObject?.filename
- }
- if localFileName == nil
- {
- return
- }
- myActivityIndicator.startAnimating()
- // Configure AWS Cognito Credentials
- let myIdentityPoolId = ""
- let credentialsProvider:AWSCognitoCredentialsProvider = AWSCognitoCredentialsProvider(regionType:AWSRegionType.-- USEast1, identityPoolId: myIdentityPoolId)
- let configuration = AWSServiceConfiguration(region:AWSRegionType.-- USEast1, credentialsProvider:credentialsProvider)
- AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
- // Set up AWS Transfer Manager Request
- let S3BucketName = "learn-swift"
- let remoteName = localFileName!
- let uploadRequest = AWSS3TransferManagerUploadRequest()
- uploadRequest.body = generateImageUrl(remoteName)
- uploadRequest.key = remoteName
- uploadRequest.bucket = S3BucketName
- uploadRequest.contentType = "image/jpeg"
- let transferManager = AWSS3TransferManager.defaultS3TransferManager()
- // Perform file upload
- transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
- dispatch_async(dispatch_get_main_queue()) {
- self.myActivityIndicator.stopAnimating()
- }
- if let error = task.error {
- print("Upload failed with error: (\(error.localizedDescription))")
- }
- if let exception = task.exception {
- print("Upload failed with exception (\(exception))")
- }
- if task.result != nil {
- let s3URL = NSURL(string: "https://s3.amazonaws.com/\(S3BucketName)/\(uploadRequest.key!)")!
- print("Uploaded to:\n\(s3URL)")
- // Remove locally stored file
- self.remoteImageWithUrl(uploadRequest.key!)
- dispatch_async(dispatch_get_main_queue()) {
- self.displayAlertMessage()
- }
- }
- else {
- print("Unexpected empty result.")
- }
- return nil
- }
- }
- func displayAlertMessage()
- {
- let alertController = UIAlertController(title: "Alert title", message: "Image has been uploaded", preferredStyle: .Alert)
- let OKAction = UIAlertAction(title: "OK", style: .Default) { (action:UIAlertAction!) in
- // Code in this block will trigger when OK button tapped.
- print("Ok button tapped");
- }
- alertController.addAction(OKAction)
- self.presentViewController(alertController, animated: true, completion:nil)
- }
- func generateImageUrl(fileName: String) -> NSURL
- {
- let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingString(fileName))
- let data = UIImageJPEGRepresentation(myImageView.image!, 0.6)
- data!.writeToURL(fileURL, atomically: true)
- return fileURL
- }
- func remoteImageWithUrl(fileName: String)
- {
- let fileURL = NSURL(fileURLWithPath: NSTemporaryDirectory().stringByAppendingString(fileName))
- do {
- try NSFileManager.defaultManager().removeItemAtURL(fileURL)
- } catch
- {
- print(error)
- }
- }
- func setupImagePickerButton()
- {
- let button = UIButton(type: UIButtonType.System) as UIButton
- let xPostion:CGFloat = 50
- let yPostion:CGFloat = 100
- let buttonWidth:CGFloat = 150
- let buttonHeight:CGFloat = 45
- button.frame = CGRectMake(xPostion, yPostion, buttonWidth, buttonHeight)
- button.backgroundColor = UIColor.lightGrayColor()
- button.setTitle("Select image", forState: UIControlState.Normal)
- button.tintColor = UIColor.blackColor()
- button.addTarget(self, action: #selector(ViewController.displayImagePickerButtonTapped) , forControlEvents: .TouchUpInside)
- self.view.addSubview(button)
- }
- func setupImageView()
- {
- myImageView = UIImageView()
- let xPostion:CGFloat = 50
- let yPostion:CGFloat = 200
- let buttonWidth:CGFloat = 200
- let buttonHeight:CGFloat = 200
- myImageView.frame = CGRectMake(xPostion, yPostion, buttonWidth, buttonHeight)
- self.view.addSubview(myImageView)
- }
- func setupUploadButton()
- {
- let rightBarButton = UIBarButtonItem(title: "Upload", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(ViewController.startUploadingImage))
- self.navigationItem.rightBarButtonItem = rightBarButton
- }
- func setUpActivityIndicator()
- {
- //Create Activity Indicator
- myActivityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
- // Position Activity Indicator in the center of the main view
- myActivityIndicator.center = view.center
- // If needed, you can prevent Acivity Indicator from hiding when stopAnimating() is called
- myActivityIndicator.hidesWhenStopped = true
- myActivityIndicator.backgroundColor = UIColor.whiteColor()
- view.addSubview(myActivityIndicator)
- }
- func displayImagePickerButtonTapped() {
- let myPickerController = UIImagePickerController()
- myPickerController.delegate = self;
- myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
- self.presentViewController(myPickerController, animated: true, completion: nil)
- }
- func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
- {
- selectedImageUrl = info[UIImagePickerControllerReferenceURL] as! NSURL
- myImageView.image = info[UIImagePickerControllerOriginalImage] as? UIImage
- myImageView.backgroundColor = UIColor.clearColor()
- myImageView.contentMode = UIViewContentMode.ScaleAspectFit
- self.dismissViewControllerAnimated(true, completion: nil)
- }
- }
I hope this blog post was valuable to you and if it was, please click on the Like button of that youtube video or subscribe(on the right side panel) to my blog, so that you can receive notification when a new free video tutorial becomes available.
Happy coding! 🙂
Comments
Post a Comment