[QUERY] How to create a SAS Uri with v12
See original GitHub issueQuery/Question Hi, I am porting my old v11 code to v12 and I find very unconvenient to generate SAS Uris with the new API. Old code was very simple:
var serviceClient = account.CreateCloudBlobClient();
var containerClient = serviceClient.GetContainerReference(containerName);
var blobClient = containerClient.GetBlockBlobReference(blobName);
var sasUri = blobClient.Uri + blobClient.GetSharedAccessSignature(new SharedAccessBlobPolicy()
{
SharedAccessExpiryTime = DateTime.UtcNow.AddHours(24),
Permissions = SharedAccessBlobPermissions.Read |
SharedAccessBlobPermissions.Delete
});
Given a blob client, I was able to get a SAS Uri without having to get the account key from the connection string, which was very conenient given that if the connection string was “UseDevelopmentStorage=true”. If I am correct the new API requires to call ToSasQueryParameters() and provide a StorageSharedKeyCredential built from account name and account key. This requires me to parse the connection string, but I don’t know exactly what to do with “UseDevelopmentStorage=true”. The new code is:
var containerClient = serviceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(blobName);
BlobSasBuilder sasBuilder = new BlobSasBuilder(BlobSasPermissions.Read | BlobSasPermissions.Delete, DateTimeOffset.UtcNow.AddHours(24))
{
BlobContainerName = containerClient.Name,
BlobName = blobName,
Resource = "b",
};
string sasBlobToken = sasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(serviceClient.AccountName, "<ACCOUNT KEY HERE, WHAT IF UseDevelopmentStorage=true?>")).ToString();
var sasUri= blobClient.Uri + "?" + sasBlobToken;
Is there a way to get the StorageSharedKeyCredential directly from a BlobServiceClient?
Thanks!
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Create a service SAS for a blob with .NET - Azure Storage
First, the code verifies that the BlobClient object is authorized with a shared key credential by checking the CanGenerateSasUri property. Then, ...
Read more >c# - How to get a Shared Access Signature on a Blob using ...
Sajeetharan's answer made me look for a BlobSasBuilder class, which actually exists. Here is how I can build one on the server:
Read more >Data Landing Zone Source | Adobe Experience Platform
Select Blob container to connect to Data Landing Zone. select-resource. Next, select Shared access signature URL (SAS) as your connection method ...
Read more >Generating Azure Blob Storage User Delegation SAS
In this post I'll show the code to generate a user delegation SAS URI with the .NET Storage SDK. And I also want...
Read more >Beginners guide and reference to Azure Blob Storage SDK ...
In this article, we are going to learn how to use the Azure blob storage SDK v12 in .Net with C# and this...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Please re-open if you have further questions.
Currently there’s a bug in
GetClient
where theStorageSharedKeyCredential
is not getting passed from the parent client. I submitted a PR to addressed this however you will have to wait until the next release of the SDK to receive the fix.Here’s a short sample based on your snippet in v12 to construct a SAS Uri, that way you don’t have to pass the
StorageSharedKeyCredential
from any of clients to the builder to generate a SAS Uri when this PR comes out.However with the current released version you would have to initialize the client with a connection string to a StorageSharedKeyCredential for now. e.g.
I believe we don’t have any plans to expose the
StorageSharedKeyCredential
from the client.Sorry for the inconvenience.