从Azure私有存储容器中获取Blob

时间:2020-05-19 15:06:24

标签: c# azure

我正在尝试从Azure的私有存储容器中获取Blob流。我从几个地方在互联网上获得了一些帮助,以提出此​​功能:

/// <summary>
/// Get a blob out of storage.
/// HEAVILY Based on Code from:
/// https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-customer-provided-key
/// </summary>
/// <param name="iSA">Name of storage account</param>
/// <param name="iSA_Key">Key for the storage account</param>
/// <param name="iContainerName">Name of the container</param>
/// <param name="iBlobName">Name of Blob to Get</param>
/// <returns>
/// A stream of the Blob
/// </returns>
public Stream getBlob(
    String iSA, String iSA_Key, String iContainerName,
    String iBlobName)
{
    Uri accountUri = new Uri("https://" + iSA + ".blob.core.windows.net");

    // https://stackoverflow.com/questions/16072709/converting-string-to-byte-array-in-c-sharp
    byte[] key = Encoding.ASCII.GetBytes(iSA_Key);

    // Specify the customer-provided key on the options for the client.
    BlobClientOptions options = new BlobClientOptions()
    {
        CustomerProvidedKey = new CustomerProvidedKey(key)
    };

    // Create a client object for the Blob service, including options.
    BlobServiceClient serviceClient = new BlobServiceClient(accountUri,
        new DefaultAzureCredential(), options);

    // Create a client object for the container.
    // The container client retains the credential and client options.
    BlobContainerClient containerClient = serviceClient.GetBlobContainerClient(iContainerName);

    // Create a new block blob client object.
    // The blob client retains the credential and client options.
    BlobClient blobClient = containerClient.GetBlobClient(iBlobName);

    Azure.Response<BlobDownloadInfo> myAR = blobClient.Download();

    BlobDownloadInfo BDI = myAR.Value;
    return BDI.Content;
}

不幸的是,当我调用blobClient.Download()时,它给了我以下错误:
Azure.RequestFailedException:“此请求无权使用此权限执行此操作...”

有什么容易解决的办法吗?

AFAIK:我正在将正确的参数传递给函数...

1 个答案:

答案 0 :(得分:0)

不确定我是否正确处理了所有MIME内容;但是,正如我的评论所述,Microsoft的示例与如何提供客户加密密钥有关。 关于如何提供存储密钥。我环顾四周并在IDE中玩耍,并能够创建一个StorageSharedKeyCredential,可以将其传递给BlobServiceClient构造函数,并对原始Microsoft示例进行一些修改:

/// <summary>
/// Get a blob out of storage.
/// HEAVILY Based on Code from:
/// https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-customer-provided-key
/// </summary>
/// <param name="iSA">Name of storage account</param>
/// <param name="iSA_Key">Key for the storage account</param>
/// <param name="iContainerName">Name of the container</param>
/// <param name="iBlobName">Name of Blob to Get</param>
/// <returns>
/// A stream of the Blob
/// </returns>
public Stream getBlob(
    String iSA, String iSA_Key, String iContainerName,
    String iBlobName)
{
    Uri accountUri = new Uri("https://" + iSA + ".blob.core.windows.net");

    // Create a client object for the Blob service
    // https://docs.microsoft.com/en-us/dotnet/api/azure.storage.storagesharedkeycredential?view=azure-dotnet
    StorageSharedKeyCredential SSKC = new StorageSharedKeyCredential(iSA, iSA_Key);
    BlobServiceClient serviceClient = new BlobServiceClient(accountUri, SSKC);

    // Create a client object for the container.
    // The container client retains the credential and client options.
    BlobContainerClient containerClient = serviceClient.GetBlobContainerClient(iContainerName);

    // Create a new block blob client object.
    // The blob client retains the credential and client options.
    BlobClient blobClient = containerClient.GetBlobClient(iBlobName);

    Azure.Response<BlobDownloadInfo> myAR = blobClient.Download();

    BlobDownloadInfo BDI = myAR.Value;
    return BDI.Content;
}

至少现在我已经从存储帐户中获得了可用的文件流。