【问题标题】:Swift 3 Azure Blob Storage Data (Image, Video) upload with SAS使用 SAS 上传 Swift 3 Azure Blob 存储数据(图像、视频)
【发布时间】:2017-08-28 07:48:20
【问题描述】:

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

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

问候,

亚当

【问题讨论】:

    标签: ios swift azure azure-blob-storage


    【解决方案1】:

    对于那些与我有同样问题的人:这是 Xcode 8 和 Swift 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 !")
        })
        }
    
    }
    

    【讨论】:

    • 必须加“?”在(容器)之后将 SAS 设置为参数。 var containerURL = "(storageUrl)(container)?(sas)"
    • 嗨,我正在做与回答中提到的相同的事情,但我想获取我上传到 blob 的图像或视频的 URL。找不到任何可以返回 URL 的函数??
    • 如何从那里获取 url?
    • @khoa 它在我的模拟器中工作,但在真实设备中没有,有什么解决方案吗?
    • 嗨@NavinBagul,它应该适用于模拟器和真实设备。我测试成功,你应该再次检查你的设备的配置。 (同时查看 ios 版本更新日志)
    猜你喜欢
    • 2020-08-12
    • 2020-11-07
    • 2015-11-30
    • 2020-04-09
    • 2018-12-17
    • 1970-01-01
    • 2020-08-14
    • 2020-01-09
    • 2014-07-22
    相关资源
    最近更新 更多