Posted by on Oct 8, 2015 in #Azure, #iOSDev | 0 comments

Let me show you how to upload data (or an image in this case) to Azure Blob Storage using the Azure Storage Client Library for iOS.

In order to get setup and add the framework to your project – head over to: http://simonjager.com/adding-azure-storage-client-library-for-ios-in-xcode-7-0/

1. Create an Azure Storage Account
To get started you need to create an Azure Storage Account and get the connection string – follow these two posts:
https://azure.microsoft.com/documentation/articles/storage-create-storage-account/
https://azure.microsoft.com/en-us/documentation/articles/storage-configure-connection-string/

2. Define your connection string
Get the connection string into your code – the way you like it. I prefer to put it into a header file, so that’s how I will do it.

connstring

Additionally I will also define the blob container name (this is the blob container which we will upload the image to). You don’t need to create the blob container beforehand, we will create it on the fly through our code.

3. Get the NSData
I will be uploading an image in this example, and in order to do that I need to extract the underlying data from the UIImage object. I can do that using the UIImageJPEGRepresentation method, which gets me an NSData object that I can upload.

imagedata

We will upload it into a blob (binary large object), that I will also define a name for (ending with “.jpeg” – as we are creating a JPEG representation of the image with UIImageJPEGRepresentation).

4. Get a blob container reference
Next up we need to get a pointer to the blob container. The blob container is a logical grouping of blobs. To do that we need to instantiate the Storage Account and the blob client – which is easily done using the connection string.
storageinit

Ideally you should not be recreating these objects with every upload, instead make sure to reuse them – code responsibly!

It’s important to know that what you are ending up with here is a reference to a blob container – which may or may not exist! So before we start uploading data to a blob in this blob container, we need to make sure that it exists!

5. Create the blob container (if needed)
There is a couple of different ways to create the blob container – (there is even a method that might be more suitable in your case – createContainerIfNotExists). I chose to go this route as I needed to set a specific blob container access type (https://msdn.microsoft.com/en-us/library/microsoft.windowsazure.storage.blob.blobcontainerpublicaccesstype.aspx).

Once you get into the completion handler – make sure to check the error object for any errors.

containercreate

6. Upload the blob
Lastly, get a reference to the blob using your (now existing) blob container. Like the blob container, this is also a reference – but since we are about to upload data into it – it will be just fine. Call the uploadFromData method and pass in your NSData object – again, make sure to check the error object for any issues.

blobupload

7. Final method
The completion handler returns an NSUrl object, that contains the URL of the blob – which is public (because I created the container with the public access type).

-Simon Jäger