question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUERY] How to create a SAS Uri with v12

See original GitHub issue

Query/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:closed
  • Created 3 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
seanmcc-msftcommented, Dec 11, 2020

Please re-open if you have further questions.

1reaction
amnguyecommented, Dec 1, 2020

Currently there’s a bug in GetClient where the StorageSharedKeyCredential 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.

var containerClient = serviceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(blobName);

// This generates a sasUri based on the blobClient information, so the containerName, blobName and resource will already be populated
Uri sasUri = blobClient.GenerateSas(BlobSasPermissions.Read | BlobSasPermissions.Delete, DateTimeOffset.UtcNow.AddHours(24));

However with the current released version you would have to initialize the client with a connection string to a StorageSharedKeyCredential for now. e.g.

var blobClient = new BlobClient(connectionString, containerName, blobName);
OR
var  blobClient = new BlobClient( blobEndpoint, sharedKeyCredential, options);

// This generates a sasUri based on the blobClient information, so the containerName, blobName and resource will already be populated
Uri sasUri = blobClient.GenerateSas(BlobSasPermissions.Read | BlobSasPermissions.Delete, DateTimeOffset.UtcNow.AddHours(24));

I believe we don’t have any plans to expose the StorageSharedKeyCredential from the client.

Sorry for the inconvenience.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found