0

I'm searching for an useful Swift 3 Azure Blob Storage example which I could use to upload some data(image, video). For now, I can insert records into my Mobile Service database and there I generate a SAS and I get it back to my iOS application. Now I need to know how to upload to Azure Blob Storage with help of that SAS. I successfully implemented the same for Android and it works, but somehow I have troubles to find any useful information for "SWIFT" and how to use the "SAS"!

Any code examples how to upload with SAS in Swift are much appreciated.

Regards,

Adam

Adam Ri
  • 347
  • 2
  • 15

1 Answers1

4

For those who have the same problem as I had: This is a working example in Xcode 8 and Swift 3. You have to include the "Azure Storage Client Library" into your project.

//Upload to Azure Blob Storage with help of SAS
func uploadBlobSAS(container: String, sas: String, blockname: String, fromfile: String ){

// If using a SAS token, fill it in here.  If using Shared Key access, comment out the following line.
var containerURL = "https://yourblobstorage.blob.core.windows.net/\(container)\(sas)"  //here we have to append sas string: + sas
    print("containerURL with SAS: \(containerURL) ")
var container : AZSCloudBlobContainer
var error: NSError?

container = AZSCloudBlobContainer(url: NSURL(string: containerURL)! as URL, error: &error)
if ((error) != nil) {
print("Error in creating blob container object.  Error code = %ld, error domain = %@, error userinfo = %@", error!.code, error!.domain, error!.userInfo);
}
else {

    let blob = container.blockBlobReference(fromName: blockname)
    blob.uploadFromFile(withPath: fromfile, completionHandler: {(NSError) -> Void in
        NSLog("Ok, uploaded !")
    })
    }

}
Adam Ri
  • 347
  • 2
  • 15
  • have to add "?" after \(container) to set SAS as parameter. var containerURL = "\(storageUrl)\(container)?\(sas)" – Khoa Aug 15 '18 at 04:25
  • Hi, I am doing same thing as mentioned in answer but I want to get the URL for the image or video I uploaded to blob. Couldn't find any function that bring back the URL any help?? – Ahsan Apr 28 '20 at 13:28
  • How to get url from there? – Ashu Apr 13 '21 at 07:44
  • @khoa its working in my simulator but not in real device, is there any solution for it ? – NavinBagul Jul 15 '21 at 08:53
  • 1
    hi @NavinBagul, it should work fine for both simulators and real devices. I successfully tested, you should check your device's configuration again. (also check ios version changelog) – Khoa Jul 16 '21 at 09:06