I'm trying to get a Blob Stream from a private Storage Container in Azure. I got some help on the internet from a few places to come up with this function:
/// <summary>
/// Get a blob out of storage.
/// HEAVILY Based on Code from:
/// https://learn.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;
}
Unfortunately, it's giving me the following error when I call blobClient.Download():
Azure.RequestFailedException: 'This request is not authorized to perform this operation using this permission...'
Is there something easy I can fix with this?
AFAIK: I am passing the correct parameters into the function...