【发布时间】:2013-03-29 06:25:21
【问题描述】:
我必须在这里遗漏一些明显的东西,但我发现自己无法进行我认为合法的向上转换。我正在尝试将数据上传到 Azure 中的 Blob 存储,并希望使用方法 CloudBlob.UploadText(string)。但是,当我访问我的 blob 时,我得到了一个 CloudBlockBlob 实例,据我所知,它继承自 CloudBlob,并且 UploadText 方法不可访问,所以我尝试了 let blob = targetBlob :> CloudBlob - 惨遭失败,消息
SmallScript.fsx(28,12): error FS0193: Type constraint mismatch. The type
CloudBlockBlob
is not compatible with type
CloudBlob
The type 'CloudBlockBlob' is not compatible with the type 'CloudBlob'
我可以使用 UploadFromStream 上传数据,但 UploadText 对我的目的来说非常方便。谁能帮我看看我错过了什么?
作为记录,如果这有帮助,这里是有效的代码:
let credentials = StorageCredentials(accountName, accountKey)
let storageAccount = CloudStorageAccount(credentials, true)
let client = storageAccount.CreateCloudBlobClient()
let containerName = "numerics"
let container = client.GetContainerReference(containerName)
container.CreateIfNotExists() |> ignore
let targetBlobName = "MiniSparseMatrix.csv"
let targetBlob = container.GetBlockBlobReference(targetBlobName)
let fileLocation = @"C:\Users\Mathias\Desktop\TestMatrix.txt"
let stream = System.IO.File.OpenRead(fileLocation)
targetBlob.UploadFromStream(stream)
stream.Close()
【问题讨论】:
-
您能否检查一下您使用的是存储客户端库 1.8 还是 2.0?我问这个的原因是因为 blob 容器上的
CreateIfNotExists()在库 2.0 中。在 1.8 中,它被命名为CreateIfNotExist()(缺少“s”)。 -
我相信这是 2012 年 10 月发布的 Azure SDK,如果这回答了您的问题。语法似乎不是问题(脚本通过并上传),失败的是 blob 类型之间的向上转换。
-
@GauravMantri 实际上你可能在某些东西上,虽然显示的脚本有效,但在其他地方容器只识别 CreateIfNotExist,没有 s。我会更深入地挖掘,您的提示可能是正确的,即库版本之间的冲突。
-
请检查您引用的是 Microsoft.WindowsAzure.StorageClient(版本 1.8)还是 Microsoft.WindowsAzure.Storage(版本 2.0)库。 2.0 版与 1.8 版有很大不同,包括 UploadText 在内的一些方法已从 2.0 版中删除。我写了一些比较这两个版本的博客文章,你可以在这里阅读:gauravmantri.com/tag/storage-client-library。 HTH。
标签: azure f# azure-storage