Open lbsong opened 1 year ago
Thank you for your feedback. This has been routed to the support team for assistance.
Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @xgithubtriage.
Author: | lbsong |
---|---|
Assignees: | - |
Labels: | `Storage`, `Service Attention`, `Client`, `customer-reported`, `question`, `needs-team-attention` |
Milestone: | - |
Adding Service team to look into this.
@lbsong You can use the BlobServiceClient which will take the SAS token as the parameter. You can check if the below sample helps.
// You can generate a service SAS or account SAS from application. The below SAS token is generated from Azure Portal
var sasToken = "?sv=2021-12-02&ss=bfqt&srt=sco&sp=rwdlacupiytfx&se=2023-03-30T15:25:29Z&st=2023-03-16T07:25:29Z&spr=https&sig=XXXXXXXXXXXXXXX";
// Update the Container Name
string containerName = "test";
// Construct a new BlobServiceClient using a AzureSasCredential.
BlobServiceClient blobServiceClient = new BlobServiceClient(new Uri(serviceUri), new AzureSasCredential(sasToken));
The issue was not about BlobServiceClient not supporting this. This is about AddBlobServiceClient not working properly with SASUrl.
Given this:
var baseBlobUri = new Uri("https://redacted.blob.core.windows.net/somecontainer?si=readwrite&spr=https&sv=2022-11-02&sr=c&sig=xxxx");
builder.Services.AddAzureClients(builder =>
{
builder.AddBlobServiceClient(baseBlobUri).WithName("TEST");
});
Then use it like this:
var blobClientFactory = app.Services.GetRequiredService<IAzureClientFactory<BlobServiceClient>>();
var blobServiceClientDi = blobClientFactory.CreateClient("TEST");
//container is already in the sas url
var blobContainerClientDi = blobServiceClientDi.GetBlobContainerClient(string.Empty);
var blobClientDi = blobContainerClientDi.GetBlobClient("text0.txt");
await blobClientDi.UploadAsync(somedata, new BlobHttpHeaders { ContentType = "text/html" });
This will fail. However the same code without DI works properly:
var blobServiceClient = new BlobServiceClient(baseBlobUri);
//container is already in the sas url
var blobContainerClient = blobServiceClient.GetBlobContainerClient(string.Empty);
var blobClient = blobContainerClient.GetBlobClient("text1.txt");
await blobClient.UploadAsync(somedata, new BlobHttpHeaders { ContentType = "text/html" });
Might be related to the token param here: https://github.com/Azure/azure-sdk-for-net/blob/e5cc0d08699b7808a82dc86b166c9a059503cb49/sdk/storage/Azure.Storage.Blobs/src/BlobClientBuilderExtensions.cs#L32
Adding this method (and using it instead) resolves this:
public static IAzureClientBuilder<BlobServiceClient, BlobClientOptions> AddBlobServiceClientSas<TBuilder>(this TBuilder builder, Uri serviceUri)
where TBuilder : IAzureClientFactoryBuilderWithCredential
{
return builder.RegisterClientFactory<BlobServiceClient, BlobClientOptions>((options, _) => new BlobServiceClient(serviceUri, options), requiresCredential: false);
}
@navba-MSFT any thoughts?
Summary based on previous answers:
builder.Services.AddAzureClients(clientBuilder =>
{
((IAzureClientFactoryBuilder)clientBuilder).RegisterClientFactory<BlobServiceClient, BlobClientOptions>(
options => new BlobServiceClient(new Uri(url), new AzureSasCredential(sas), options))
.WithName("...");
});
@th3ragex typically SAS urls are not provided in two pieces - the SAS and the url. They come all as one. It would be nice if it just supported this standard scenario.
Library name
Azure.Storage.Blobs
Please describe the feature.
why does AddBlobServiceClient not support using SAS? https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/storage/Azure.Storage.Blobs/src/BlobClientBuilderExtensions.cs