Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUERY] How to create a SAS Uri with v12 #16979

Closed
bragma opened this issue Nov 15, 2020 · 8 comments
Closed

[QUERY] How to create a SAS Uri with v12 #16979

bragma opened this issue Nov 15, 2020 · 8 comments
Assignees
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)

Comments

@bragma
Copy link

bragma commented Nov 15, 2020

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!

@ghost ghost added needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. customer-reported Issues that are reported by GitHub users external to the Azure organization. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Nov 15, 2020
@bragma
Copy link
Author

bragma commented Nov 16, 2020

I have to beg pardon since the question has already been presented here and also here but both issues are more than 6 months old and have NOT been addressed yet...

@jsquire jsquire added Client This issue points to a problem in the data-plane of the library. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files) labels Nov 16, 2020
@ghost ghost removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Nov 16, 2020
@jsquire
Copy link
Member

jsquire commented Nov 16, 2020

Thank you for your feedback. Tagging and routing to the team best able to assist.

@ghost
Copy link

ghost commented Nov 16, 2020

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.

Issue Details
Description:

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!

Author: bragma
Assignees: -
Labels:

Client, Service Attention, Storage, customer-reported, needs-team-attention, needs-triage, question

Milestone: -

@seanmcc-msft
Copy link
Member

@amnguye can you take a look?

@amnguye
Copy link
Member

amnguye commented 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.

@bragma
Copy link
Author

bragma commented Dec 1, 2020

Ok, thanks for the answer. I'll wait for the next release, I have to since I am also blocked by a nasty memory leak with v12.

@amnguye
Copy link
Member

amnguye commented Dec 7, 2020

This sample I provided above should currently work with the latest released preview version of Azure.Storage.Blobs .

@seanmcc-msft
Copy link
Member

Please re-open if you have further questions.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Client This issue points to a problem in the data-plane of the library. customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-team-attention Workflow: This issue needs attention from Azure service team or SDK team question The issue doesn't require a change to the product in order to be resolved. Most issues start as that Service Attention Workflow: This issue is responsible by Azure service team. Storage Storage Service (Queues, Blobs, Files)
Projects
None yet
Development

No branches or pull requests

4 participants