如何从 Azure 容器中的 blob 获取所有 URL 的列表?
How to get a list of all URLs from blobs in a container in Azure?
到目前为止,我列出了 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;
}
}
}
我假设你可以按照 blob URL 构造的约定来生成相关的 URL
https://myaccount.blob.core.windows.net/mycontainer/myblob
https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url
如果您使用的是最新的 azure 存储包 Azure.Storage.Blobs 12.8.0,有两种方法可以获取 blob url。
1.You可以自己搭建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.Another就是获取到blob名称后,可以使用blob名称获取BlobClient,然后可以从BlobClient中获取bloburl。代码如下:
//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 });
}
到目前为止,我列出了 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;
}
}
}
我假设你可以按照 blob URL 构造的约定来生成相关的 URL
https://myaccount.blob.core.windows.net/mycontainer/myblob
https://docs.microsoft.com/en-us/rest/api/storageservices/copy-blob-from-url
如果您使用的是最新的 azure 存储包 Azure.Storage.Blobs 12.8.0,有两种方法可以获取 blob url。
1.You可以自己搭建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.Another就是获取到blob名称后,可以使用blob名称获取BlobClient,然后可以从BlobClient中获取bloburl。代码如下:
//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 });
}