Swift 3 使用 SAS 上传 Azure Blob 存储数据(图像、视频)
Swift 3 Azure Blob Storage Data (Image, Video) upload with SAS
我正在寻找一个有用的 Swift 3 Azure Blob 存储示例,我可以使用它来上传一些数据(图像、视频)。现在,我可以将记录插入到我的移动服务数据库中,然后生成一个 SAS,然后将其返回到我的 iOS 应用程序。现在我需要知道如何借助该 SAS 上传到 Azure Blob 存储。我成功地为 Android 实现了相同的功能并且它有效,但不知何故我很难找到 "SWIFT" 的任何有用信息以及如何使用 "SAS"!
非常感谢 Swift 中如何使用 SAS 上传的任何代码示例。
此致,
亚当
对于那些和我有同样问题的人:这是 Xcode 8 和 Swift 3 中的一个工作示例。您必须将 "Azure Storage Client Library" 包含到您的项目中。
//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 !")
})
}
}
我正在寻找一个有用的 Swift 3 Azure Blob 存储示例,我可以使用它来上传一些数据(图像、视频)。现在,我可以将记录插入到我的移动服务数据库中,然后生成一个 SAS,然后将其返回到我的 iOS 应用程序。现在我需要知道如何借助该 SAS 上传到 Azure Blob 存储。我成功地为 Android 实现了相同的功能并且它有效,但不知何故我很难找到 "SWIFT" 的任何有用信息以及如何使用 "SAS"!
非常感谢 Swift 中如何使用 SAS 上传的任何代码示例。
此致,
亚当
对于那些和我有同样问题的人:这是 Xcode 8 和 Swift 3 中的一个工作示例。您必须将 "Azure Storage Client Library" 包含到您的项目中。
//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 !")
})
}
}