C# 如何在Azure中从容器中的blob获取所有URL的列表?
到目前为止,我正在列出Azure Blob存储中容器中Blob的名称和创建日期C# 如何在Azure中从容器中的blob获取所有URL的列表?,c#,azure,blob,azure-storage-blobs,C#,Azure,Blob,Azure Storage Blobs,到目前为止,我正在列出Azure Blob存储中容器中Blob的名称和创建日期 现在我想添加来自相同blob的URL列表。我做了很多研究,但没有找到真正有用的东西 是否可以使用我用于其他blob属性的相同方法实现这一点,或者是否有其他方法 我的代码: using System.Threading.Tasks; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.Extensions.Http; using Microsoft.
现在我想添加来自相同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;
}
}
}
使用System.Threading.Tasks;
使用Microsoft.Azure.WebJobs;
使用Microsoft.Azure.WebJobs.Extensions.Http;
使用Microsoft.AspNetCore.Http;
使用Microsoft.Extensions.Logging;
使用Azure.Storage.Blobs;
使用制度;
使用System.Collections.Generic;
命名空间getBlobData
{
//定义数据传输对象(DTO)
公共类容器信息
{
公共字符串名
{
获得;设置;
}
公共日期时间偏移量?CreatedOn
{
获得;设置;
}
}
公共静态类GetBlobData
{
[函数名(“getBlobData”)]
公共静态异步任务运行(
[HttpTrigger(AuthorizationLevel.Function,“get”,Route=null)]HttpRequest请求,
ILogger日志)
{
//连接到存储帐户中的容器
//把水滴放进去
string connection_string=Environment.GetEnvironmentVariable(“AZURE_存储_connection_string”);
BlobServiceClient BlobServiceClient=新BlobServiceClient(连接\u字符串);
BlobContainerClient containerClient=blobServiceClient.GetBlobContainerClient(“容器”);
var response=containerClient.GetBlobsAsync();
//获取blob的名称和创建日期
//返回DTO
var res=新列表();
等待foreach(响应中的var项目)
{
res.Add(新容器信息{Name=item.Name,CreatedOn=item.Properties.CreatedOn});
}
返回res;
}
}
}
我假设您可以使用blob URL构造的约定来生成相关URL
https://myaccount.blob.core.windows.net/mycontainer/myblob
如果您使用的是最新的azure存储包,有两种方法可以获取blob url 1.您可以自己构建
blobURL
。首先,需要获取blob容器url
,然后将blob容器url与blob名称
连接起来,代码如下:
//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 });
}
//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 });
}
//其他代码
BlobContainerClient containerClient=blobServiceClient.GetBlobContainerClient(“容器”);
//在此处获取容器url
字符串container_url=containerClient.Uri.ToString();
var response=containerClient.GetBlobsAsync();
//获取blob的名称和创建日期
//返回DTO
var res=新列表();
等待foreach(响应中的var项目)
{
//在这里,您可以将容器url与blob名称连接起来
string blob_url=container_url+“/”+item.Name;
res.Add(新容器信息{Name=item.Name,CreatedOn=item.Properties.CreatedOn});
}
2.另一个是,在获得blob名称后,可以使用blob名称获取blob客户端,然后可以从blob客户端获取blob url。代码如下:
//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 });
}
//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 });
}
//其他代码
BlobContainerClient containerClient=blobServiceClient.GetBlobContainerClient(“容器”);
var response=containerClient.GetBlobsAsync();
//在此定义BlobClient。
BlobClient BlobClient=null;
//获取blob的名称和创建日期
//返回DTO
var res=新列表();
等待foreach(响应中的var项目)
{
//在这里,您可以使用blob名称获得blob客户端
blobClient=containerClient.GetBlobClient(item.Name);
//然后可以使用BlobClient获取BlobURL
字符串blob_url=blobClient.Uri.ToString();
res.Add(新容器信息{Name=item.Name,CreatedOn=item.Properties.CreatedOn});
}