[英]How to get a list of all URLs from blobs in a container in Azure?
So far Im listing the names and creation dates of the blobs from the container in Azure Blob Storage.到目前为止,我列出了 Azure Blob Storage 中容器中 blob 的名称和创建日期。
Now I want to add a list of the URLs from the same blobs.现在我想添加来自相同 blob 的 URL 列表。 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?是否可以使用我用于其他 blob 属性的相同方法来实现这一点,还是有其他方法?
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;
}
}
}
If you're using the latest azure storage package Azure.Storage.Blobs 12.8.0 , there are 2 ways to fetch the blob url. 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. 1.您可以自己构建
blob url
。 First, you need to get the blob container url
, then concatenate blob container url with the blob name
, the code like below:首先,您需要获取
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.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. 2.还有一个就是得到blob名字后,可以使用blob名字来获取BlobClient,然后就可以从BlobClient获取blob url。 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 });
}
I assume you can conventions of blob URL construction, to generate the relevant URL我假设您可以约定 blob URL 构造,以生成相关的 URL
https://myaccount.blob.core.windows.net/mycontainer/myblob
https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.