【问题标题】:How to get a list of all URLs from blobs in a container in Azure?如何从 Azure 容器中的 blob 中获取所有 URL 的列表?
【发布时间】:2021-05-06 11:41:16
【问题描述】:

到目前为止,我列出了 Azure Blob 存储中容器中 Blob 的名称和创建日期。
现在我想添加来自相同 blob 的 URL 列表。我做了很多研究,但我真的找不到有用的东西。
是否可以使用我用于其他 blob 属性的相同方法来实现这一点,还是有其他方法?

我的代码:

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;
        }

    }
}

【问题讨论】:

    标签: c# azure blob azure-blob-storage


    【解决方案1】:

    如果您使用的是最新的 azure 存储包Azure.Storage.Blobs 12.8.0,则有 2 种方法可以获取 blob url。

    1.您可以自己构建blob url。首先,您需要获取blob container url,然后将blob 容器url 与blob name 连接起来,代码如下:

            //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.另外一个就是你得到blob名字后,可以使用blob名字来获取BlobClient,然后就可以从BlobClient获取blob url。代码如下:

            //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 });
            }
    

    【讨论】:

      【解决方案2】:

      我假设您可以按照 blob URL 构造的约定来生成相关的 URL

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

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

      【讨论】:

        猜你喜欢
        • 2015-11-10
        • 2020-10-09
        • 2022-01-15
        • 2018-07-06
        • 1970-01-01
        • 2019-01-18
        • 1970-01-01
        • 2014-09-15
        • 2017-10-27
        相关资源
        最近更新 更多