'Write file to blob storage and save the SaS URL using C#

I am trying to create an Azure Function that create files in blob storage and then save a pre-signed blob file url that is generated dynamically in an azure table so that we can return blob file url to the client program to open.

I am able to create the files in blob storage and save the urls. Right now, the code makes the file urls public, I am not sure how can I make the current code generate SaS url instead of public url and save it to the azure table.

I didn't see any example that shows the usage of CloudBlobClient and SaS. Appreciate any help.

[FunctionName("CreateFiles")]
public static async void Run([QueueTrigger("JobQueue", Connection = "")]string myQueueItem,
    [Table("SubJobTable", Connection = "AzureWebJobsStorage")] CloudTable subJobTable,
    ILogger log)
{
    Job job = JsonConvert.DeserializeObject<Job>(myQueueItem);

    var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
    CloudBlobClient cloudBlobClient = storageAccount.CreateCloudBlobClient();
    string containerName = $"{job.Name.ToLowerInvariant()}{Guid.NewGuid().ToString()}";
    CloudBlobContainer cloudBlobContainer =
        cloudBlobClient.GetContainerReference(containerName);
    cloudBlobContainer.CreateIfNotExists();

    BlobContainerPermissions permissions = new BlobContainerPermissions
    {
            PublicAccess = BlobContainerPublicAccessType.Blob
    };
    cloudBlobContainer.SetPermissions(permissions);

    string localPath = "./data/";
    string localFileName = $"{job.Id}.json";
    string localFilePath = Path.Combine(localPath, localFileName);
    File.WriteAllText(localFilePath, myQueueItem);
    CloudBlockBlob cloudBlockBlob = cloudBlobContainer.GetBlockBlobReference(localFileName);
    log.LogInformation("Uploading to Blob storage as blob:\n\t {0}\n", cloudBlockBlob.Uri.AbsoluteUri);
    cloudBlockBlob.UploadFromFile(localFilePath);

    // update the table with file uri
    DynamicTableEntity entity = new DynamicTableEntity(job.Id, job.PracticeId);
    entity.Properties.Add("FileUri", new EntityProperty(cloudBlockBlob.Uri.AbsoluteUri));
    entity.Properties.Add("Status", new EntityProperty("Complete"));
    TableOperation mergeOperation = TableOperation.InsertOrMerge(entity);
    subJobTable.Execute(mergeOperation);
} 


Solution 1:[1]

It looks like that your code is making use of the older version of the SDK (Microsoft.Azure.Storage.Blob). If that's the case, then you would need to use GetSharedAccessSignature method in CloudBlob to generate a shared access signature token.

Your code would be something like:

...
cloudBlockBlob.UploadFromFile(localFilePath);
var sasToken = cloudBlockBlob. GetSharedAccessSignature(sas-token-parameters);
var sasUrl = "${cloudBlockBlob.Uri.AbsoluteUri}?${sasToken}";//Add question mark only if sas token does not have it.
...

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1