简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM