I'm using a Blob trigger Azure function to get Blob files data whenever any file is uploaded to the Container.
public static void Run(Stream myBlob,string BlobTrigger,System.Uri uri, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes + URL: {uri}");
}
Using the above method, I'm able to get the URL of the Item uploaded. Currently, the above code is generated using Develop in Portal option provided by Azure.
Is there a way I can get SAS URL of the Blob file that has been uploaded ?
After reproducing from our end. Here is how I could able to achieve when blob is uploaded each time.
using System;
using System.IO;
using Azure.Storage;
using Azure.Storage.Blobs;
using Azure.Storage.Sas;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;
namespace FunctionApp10
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "ConnectionStore")]Stream myBlob, Uri uri, string name, ILogger log)
{
log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
BlobSasBuilder blobSasBuilder = new BlobSasBuilder()
{
BlobContainerName = "<YOURCONTAINERNAME>",
BlobName = name,
ExpiresOn = DateTime.UtcNow.AddMinutes(5),
};
blobSasBuilder.SetPermissions(BlobSasPermissions.All);
string accountName = "<YOURACCOUNTNAME>";
string accountKey = "<YOURACCOUNTKEY>";
var sasToken = blobSasBuilder.ToSasQueryParameters(new StorageSharedKeyCredential(accountName, accountKey)).ToString();
var sasUrl = uri.AbsoluteUri + "?" + sasToken;
Console.WriteLine(sasUrl);
}
}
}
RESULT :
In storage account
REFERENCES: Generate SAS token c# programmatically