Search code examples
azure-functionsazure-blob-storageazure-triggerssas-token

Unable to get Blob SAS URL using Azure Function Blob trigger


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 ?


Solution

  • 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 :

    enter image description here

    In storage account

    enter image description here

    enter image description here

    REFERENCES: Generate SAS token c# programmatically