Home > Software engineering >  Upload multiple files to Azure Blob storage using container's SAS token/URL
Upload multiple files to Azure Blob storage using container's SAS token/URL

Time:12-29

I am able to generate the SAS token for single file using this code in C#

BlobClient blobClient = this.container.GetBlobClient(blobPath);
BlobSasBuilder sasBuilder = new BlobSasBuilder()
{
    BlobName = blobClient.Name,
    BlobContainerName = this.container.Name,
    Resource = "c", // b for blob, c for container
    StartsOn = DateTimeOffset.UtcNow,
    ExpiresOn = DateTimeOffset.UtcNow.AddHours(this.sasTokenExpiryHours),
};
sasBuilder.SetPermissions(BlobSasPermissions.Write);
Uri sasUri = blobClient.GenerateSasUri(sasBuilder);
return sasUri.AbsoluteUri;

Using the token generated, I simply send a PUT request with binary body. It works fine for single file.

Is there any way I can generate the SAS token for container and upload multiple files using the container SAS token before the token expires?

CodePudding user response:

Following @GauravMantri's suggestion,

BlobSasBuilder sasBuilder = new BlobSasBuilder()
{
    BlobContainerName = this.container.Name,
    Resource = "c", // b for blob, c for container
    StartsOn = DateTimeOffset.UtcNow,
    ExpiresOn = DateTimeOffset.UtcNow.AddMinutes(2),
};
sasBuilder.SetPermissions(BlobSasPermissions.Write);
Uri sasUri = this.container.GenerateSasUri(sasBuilder);
return sasUri.AbsoluteUri;

This gives the SAS token [http://{the URL}/{theSASToken}] and simply needed to update the URL as http://{the URL}/file1?{theSASToken}

  • Related