简体   繁体   English

如何从 Azure Blob 存储 C# 中的文件 URL 获取容器名称

[英]How to get container name from file URL in Azure Blob Storage C#

We are able to get container name using the code below:我们可以使用以下代码获取容器名称:

new CloudBlobContainer(url).Name

But this method works only if the URL is container base URL.但此方法仅在 URL 是容器基础 URL 时才有效。

If we are trying to pass the URL of a file and try to get the container name, the code will not return data as expected.如果我们尝试传递文件的 URL 并尝试获取容器名称,代码将不会按预期返回数据。 In this case, we have to use the CloudBlockBlob object.在这种情况下,我们必须使用 CloudBlockBlob object。

new CloudBlockBlob(new Uri(Url)).Container.Name

Is there any method to get a container name that will work for URLs listed below:有什么方法可以获取适用于下面列出的 URL 的容器名称:

http://127.0.0.1:10000/devstoreaccount1/10d59357-b4d1-41e8-ba2a-d92964e1ac53

http://127.0.0.1:10000/devstoreaccount1/10d59357-b4d1-41e8-ba2a-d92964e1ac53/temp/1.txt

Older SDK (9.3.3)旧版 SDK (9.3.3)

If you're using older SDK (9.3.3), You can use BlobClient and the name of the blob container will be available in BlobContainerName property.如果您使用的是较旧的 SDK (9.3.3),您可以使用BlobClient并且 Blob 容器的名称将在BlobContainerName属性中可用。

BlobClient client = new BlobClient(new Uri(url));

在此处输入图片说明


在此处输入图片说明

Newer SDK (12.2.0)较新的 SDK (12.2.0)

For newer SDK (12.2.0), you can use BlobUriBuilder and the name of the blob container will be available in BlobContainerName property.对于较新的 SDK (12.2.0),您可以使用BlobUriBuilder并且 Blob 容器的名称将在BlobContainerName属性中可用。

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(new Uri(url));

在此处输入图片说明


在此处输入图片说明

string blobUri = "http://127.0.0.1:10000/devstoreaccount1/10d59357-b4d1-41e8-ba2a-d92964e1ac53/temp/1.txt";    
Uri uri = new Uri(blobUri);

string containerName = uri.Segments[1];

string containerUri = uri.GetLeftPart(UriPartial.Authority) + containerName;

This should help you这应该可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 C# 中的 Azure.Storage.Blobs 以 ByteArray 格式从 Azure 存储 blob 中获取文件 - How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C# 如何从c#中的URL将文件直接上传到azure blob? - How to upload a file directly to azure blob from the URL in c#? 如何使用 c# 获取 Azure Blob 存储容器中现有目录的列表? - How to get a list of existing directories in Azure Blob Storage container using c#? 从URL提取TIF并将其移到C#中的Azure Blob存储中 - Taking a TIF from a URL and moving it into Azure blob storage in C# 从Azure存储获取Blob文件名的最快方法 - Fastest way to get the blob file name from Azure storage 如何使用 C# 将文件上传到 Azure Blob 存储? - How to upload file into Azure Blob Storage using C#? 如何使用 Blob 存储中的 Azure 函数和 C# 从 JSON 文件中获取数据以及将 JSON 数据存储为 Azure 表存储中的列 - How to get data from a JSON file using Azure functions and C# which is in Blob storage and Storing JSON data as columns in Azure table storage 如何使用 c# 从 azure blob 存储中检索 xml 文件 - how to retrieve an xml file from azure blob storage using c# C#Azure Blob存储/获取blob大小 - C# Azure Blob Storage / Get blob size C#从azure存储容器路径解压缩文件 - C# unzip file from azure storage container path
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM