Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I upload an image from my ios app to my bucket stored on aws s3 and do all of that in Swift?

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?

like image 229
user3766930 Avatar asked Mar 04 '16 00:03

user3766930


People also ask

How do I upload pictures to my Galaxy S3 bucket?

To upload a photo to an album in the Amazon S3 bucket, the application's addPhoto function uses a file picker element in the web page to identify a file to upload. It then forms a key for the photo to upload from the current album name and the file name.

Can I store images in S3 bucket?

You can create a dataset using images stored in an Amazon S3 bucket. With this option, you can use the folder structure in your Amazon S3 bucket to automatically classify your images. You can store the images in the console bucket or another Amazon S3 bucket in your account.


1 Answers

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

like image 52
Rohan Dubal Avatar answered Oct 11 '22 12:10

Rohan Dubal