2017-04-03 54 views
-1

我正在寻找一个有用的Swift 3 Azure Blob存储示例,我可以使用它来上传一些数据(图像,视频)。现在,我可以将记录插入到我的移动服务数据库中,然后生成一个SAS,然后将其恢复到我的iOS应用程序。现在我需要知道如何在SAS的帮助下上传到Azure Blob存储。我成功地实现了Android的相同,它的工作原理,但不知何故,我有麻烦找到任何有用的信息“SWIFT”,以及如何使用“SAS”!Swift 3使用SAS上传Azure Blob存储数据(图像,视频)

任何代码示例如何在Swift中使用SAS上传非常感谢。

问候,

亚当

回答

2

对于那些因为我有谁有同样的问题:这是在Xcode 8和斯威夫特3.工作的例子。必须要“Azure存储客户端库”包括成你的项目。

//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 !") 
    }) 
    } 

}