Uploading to Google Cloud Storage from iOS app
I am using Google appengine as well as Google cloud storage to store images uploaded from my iOS app (IntoMovies: Goodreads for movies and a community for movie and TV show fans — ). And, it took me a bit of head scratching to get image upload working from iOS directly to google cloud storage. Also, I didn’t want to use google’s iOS library which I think is not very clean, has way more code than it’s really needed, and is too complicated for simple use case.
I looked around for a easier way to upload images and finally got it working and wanted to share with other developers as I saw lot of developers asking questions related to it in stackoverflow.com and with not much of answers.
So, here’s what I did. I am assuming that you have google app engine project created and Google Cloud Storage is enabled in console’s APIs & auth section. Now simple 3 steps
- Create a bucket in Cloud Storage section of your project’s console
- Create upload servlet or controller in appengine server that does 2 things:
a) GET request handler that uses appengine’s blobstore service to create the upload URL based on bucket name and other options and return to the client app.
b) POST request handler which is basically a callback from google cloud storage once image is uploaded to cloud store.
Here’s the code for our Upload servlet,
3. Now the fun part, iOS client. Here I have a single method uploadImage that first gets the upload URL and uploads image data to that URL using multipart upload. Here’s the client code
One thing to note here is the form data that we pass in such as ‘name’ and ‘filename’ these should match the param names that you read in our POST request handler. Also, we are using blobstore to upload images to google cloud storage that way we are not limited by timeout limit on appengine instances. Though, we might need to have a cron job to remove keys from blob store. Hope this helps!! Feel free to ask questions!
Originally published at narup.blogspot.com on November 12, 2014.