使用其URI获取对Blob的引用

时间:2019-07-17 16:32:15

标签: c# azure-storage-blobs

我们如何通过URI获取Blob引用?

我正在像这样引用blob:

        var blob = new CloudBlockBlob(new Uri("https://xxxx.blob.core.windows.net/input/DisabledFlights[1].cache"));

我获得此URL的方法是通过门户网站:

enter image description here

尝试下载时:

            await blob.DownloadToStreamAsync(stream);

我遇到以下异常:

[7/17/2019 4:25:27 PM] Executed 'sFtpSender' (Failed, Id=4943c931-5850-4dc8-abe4-c111ed6bbfa9)
[7/17/2019 4:25:27 PM] System.Private.CoreLib: Exception while executing function: sFtpSender. Microsoft.WindowsAzure.Storage: The specified resource does not exist.

我在做什么错?如何获得对Blob的引用?

2 个答案:

答案 0 :(得分:1)

尝试一下:

var blobName = new CloudBlockBlob(new Uri("https://xxxx.blob.core.windows.net/input/DisabledFlights[1].cache")).Name;

CloudBlockBlob blockBlob = _container.GetBlockBlobReference(blobName);

其中_containerCloudBlobContainer 然后

await blockBlob.DownloadToStreamAsync(stream);

答案 1 :(得分:1)

根据错误消息,我认为blob在私有容器中。

有两种方法可以使用uri获取Blob引用。

方法1 :将容器访问级别更改为Blob级别或容器级别,然后可以直接使用以下代码:

var blob = new CloudBlockBlob(new Uri("https://xxxx.blob.core.windows.net/input/DisabledFlights[1].cache"));

await blob.DownloadToStreamAsync(stream);

方法2 :如果要将容器保留为私有,则在启动CloudBlockBlob()时,应在其中指定StorageCredentials,如下所示:

//besides using sas token, you can also use other way to construct StorageCredentials, like using account_name / account_key.
var blob = new CloudBlockBlob(new Uri("https://xxxx.blob.core.windows.net/input/DisabledFlights[1].cache"), ,new StorageCredentials("sasToken"));

await blob.DownloadToStreamAsync(stream);