gpt4 book ai didi

c# - CloudBlockBlob 的 URI 在 C# 中给出特殊字符

转载 作者:行者123 更新时间:2023-11-30 22:54:00 26 4
gpt4 key购买 nike

我有与 Azure Blob 交互的简单代码,如下所示。
我想获取上传文件的 URL。
假设如果在我的 blob 存储中,我有一个容器,其中有多个文件夹,我需要获取文件的路径。因此,为了获取该路径,我尝试使用 blockBlob.Uri 参数。

using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string storageAccConnString = "Connection string";
CloudStorageAccount storageAccount = "Account Name";
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
string folderPath "Folder1" + Path.DirectorySeparatorChar + "Folder2" + Path.DirectorySeparatorChar + "Folder3";
CloudBlobContainer container = blobClient.GetContainerReference(folderPath);
CloudBlockBlob blockBlob = container.GetBlockBlobReference("Local system filePath which needs to be uploaded to Blob");
Console.WriteLine("URI : " + blockBlob.Uri);
Console.WriteLine("URI : " + blockBlob.Uri.ToString());
Console.WriteLine("URI : " + blockBlob.Uri.OriginalString);
Console.ReadLine();
// I didnt write the upload code as my question was regarding URI
}
}
}

我收到了对此的响应,但我收到的不是“/”或“\”等文件夹分隔符,而是 %5C .

URI : https://storageaccountname.blob.core.windows.net/Folder1%5CFolder2%5Folder3/Test.zip

我该如何纠正这个问题。任何帮助都会有用。谢谢。

最佳答案

您的代码存在一些问题:

  • 首先,Blob 存储有两级层次结构:容器和 Blob。您为容器创建引用的方式不正确。由于您的容器名称包含 URL 保留字符 (\),因此它的 URL 编码为 %5C
  • 接下来,如果要上传容器内虚拟文件夹内的文件,则需要将虚拟文件夹的路径附加到 blob 的名称中。例如,假设您有一个名为 files 的 Blob 容器,并且您想要上传名为 myfile.txt 的文件,但将其放入 Folder1/Folder2/Folder3 虚拟文件夹,您可以这样做:
static void Main(string[] args)
{
string storageAccConnString = "Connection string";
CloudStorageAccount storageAccount = "Account Name";
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference('files');
var blobName = "Folder1/Folder2/Folder3" + "myfile.txt";
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobName);
Console.WriteLine("URI : " + blockBlob.Uri);
Console.WriteLine("URI : " + blockBlob.Uri.ToString());
Console.WriteLine("URI : " + blockBlob.Uri.OriginalString);
Console.ReadLine();
// I didnt write the upload code as my question was regarding URI
}

关于c# - CloudBlockBlob 的 URI 在 C# 中给出特殊字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56664262/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com