[英]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.