Uploading to Google Cloud Storage from iOS app

purans
Google Cloud - Community
2 min readNov 12, 2014

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

  1. Create a bucket in Cloud Storage section of your project’s console
  2. 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,

https://gist.github.com/narup/975b9f6c58e4bc5c41db

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

https://gist.github.com/narup/20f089b4c75c7d5c2570

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.

--

--

purans
Google Cloud - Community

Many reads with 0 writes. Co-Founder at Phil, Inc - Smarter Prescriptions, https://phil.us