2

So far Im listing the names and creation dates of the blobs from the container in Azure Blob Storage.
Now I want to add a list of the URLs from the same blobs. I did a lot of research but I can't really find something that is of use.
Is it possible to achieve this with the same method I used for the other blob properties or is there another way?

My code:

using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Azure.Storage.Blobs;
using System;
using System.Collections.Generic;

namespace getBlobData
{

    // Define data transfer objects (DTO)
    public class ContainerInfo
    {
        public string Name
        {
            get; set;
        }

        public DateTimeOffset? CreatedOn
        {
            get; set;
        }
    }

    public static class GetBlobData
    {
        [FunctionName("getBlobData")]
        public static async Task<List<ContainerInfo>> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            // Connect to container in storage account
            // Get Blobs inside of it
            string connection_string = Environment.GetEnvironmentVariable("AZURE_STORAGE_CONNECTION_STRING");
            BlobServiceClient blobServiceClient = new BlobServiceClient(connection_string);
            BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("container");
            var response = containerClient.GetBlobsAsync();

            // Get name and creation date of Blobs
            // Return DTOs
            var res = new List<ContainerInfo>();
            await foreach (var item in response)
            {
                res.Add(new ContainerInfo { Name = item.Name, CreatedOn = item.Properties.CreatedOn });
            }

            return res;
        }

    }
}

2 Answers 2

3

If you're using the latest azure storage package Azure.Storage.Blobs 12.8.0, there are 2 ways to fetch the blob url.

1.You can build the blob url by yourself. First, you need to get the blob container url, then concatenate blob container url with the blob name, the code like below:

        //other code
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("container");

        //get the container url here
        string container_url = containerClient.Uri.ToString();
        var response = containerClient.GetBlobsAsync();

        // Get name and creation date of Blobs
        // Return DTOs
        var res = new List<ContainerInfo>();
        await foreach (var item in response)
        {
            //here you can concatenate the container url with blob name
            string blob_url = container_url + "/" + item.Name;                

            res.Add(new ContainerInfo { Name = item.Name, CreatedOn = item.Properties.CreatedOn });
        }

2.Another is that after you get the blob name, you can use the blob name to get the BlobClient, then you can get the blob url from BlobClient. Code like below:

        //other code
        BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("container");
        var response = containerClient.GetBlobsAsync();

        //define a BlobClient here.
        BlobClient blobClient = null;

        // Get name and creation date of Blobs
        // Return DTOs
        var res = new List<ContainerInfo>();
        await foreach (var item in response)
        {
            //here you can get a BlobClient by using blob name
            blobClient = containerClient.GetBlobClient(item.Name); 

            //then you can get the blob url by using BlobClient
            string blob_url = blobClient.Uri.ToString();              

            res.Add(new ContainerInfo { Name = item.Name, CreatedOn = item.Properties.CreatedOn });
        }
Sign up to request clarification or add additional context in comments.

Comments

0

I assume you can conventions of blob URL construction, to generate the relevant URL

https://myaccount.blob.core.windows.net/mycontainer/myblob

https://learn.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.