How to upload images to Azure using Swift How to upload images to Azure using Swift azure azure

How to upload images to Azure using Swift


Here is a simple example.

1- Start here: https://azure.microsoft.com/en-us/documentation/articles/storage-ios-how-to-use-blob-storage/

2- Get the SDK

3- Here is the code:

let account = AZSCloudStorageAccount(fromConnectionString:AZURE_STORAGE_CONNECTION_STRING) //I stored the property in my header filelet blobClient: AZSCloudBlobClient = account.getBlobClient()let blobContainer: AZSCloudBlobContainer = blobClient.containerReferenceFromName("<yourContainerName>")blobContainer.createContainerIfNotExistsWithAccessType(AZSContainerPublicAccessType.Container, requestOptions: nil, operationContext: nil) { (NSError, Bool) -> Void in   if ((NSError) != nil){      NSLog("Error in creating container.")   }   else {      let blob: AZSCloudBlockBlob = blobContainer.blockBlobReferenceFromName(<nameOfYourImage> as String) //If you want a random name, I used let imageName = CFUUIDCreateString(nil, CFUUIDCreate(nil))                let imageData = UIImagePNGRepresentation(<yourImageData>)      blob.uploadFromData(imageData!, completionHandler: {(NSError) -> Void in      NSLog("Ok, uploaded !")      })    }}

Enjoy :)


You have to use their REST API, but they're working on an SDK right now.

There are a couple of examples of using their REST API on iOS. A cursory search brings up: Uploading to azure blob storage from SAS URL returns 404 status

There is also this example on Github - https://github.com/Ajayi13/BlobExample-Swift


In iOS 11 and Swift 4, you can do like this:

private let containerName = "<Your Name>"private let connectionString = "<Your String>"do {    let account = try AZSCloudStorageAccount(fromConnectionString: connectionString)    let blobClient = account?.getBlobClient()    let blobContainer = blobClient?.containerReference(fromName: containerName)    let currentDate = Date()    let fileName = String(currentDate.timeIntervalSinceReferenceDate)+".jpg"    let blob = blobContainer?.blockBlobReference(fromName: now)    blob?.upload(from: imageData, completionHandler: {(error)->Void in        print(now, "uploaded!") // imageData is the data you want to upload    })} catch {    print(error)}

This is just an example. Hope it helps.