我正在使用 Azure SDK for .Net(版本 9.3.1,Platform .NET-Standard 2.0)来处理 Azure Blob 存储,并且在引用 blob 名称中有空格的块 blob 时遇到了麻烦。我已通过 Azure 存储资源管理器 1.6.1 将块 blob JSON Test.json 上传到私有容器中。
根据 Azure 存储资源管理器的 Blob 属性:
Name: `JSON Test.json`
URI: `https://<myaccountname>/<mycontainername>/JSON%20Test.json`
现在,我正在尝试CloudBlockBlob.ExistsAsync()
通过传递给GetBlockBlobReference
非编码文件名 JSON Test.json 的方法检查该 Blob 是否存在
结果得到 FALSE。
现在,我正在不同的容器中以编程方式创建一个 blob,传递相同的未编码文件名,使用相同的文件名GetBlockBlobReference
并创建一个带有编码文件名的 blob。
Name: `JSON%20Test.json`
URI: `https://<myaccountname>/<mycontainername2>/JSON%20Test.json`
我做错了什么?为什么在使用非编码文件名引用它时找不到通过 Azure 存储资源管理器创建的名称中带有空格的块 blob?以编程方式创建块 blob 时,为什么要通过网络对文件名进行编码?
请帮忙。
非常感谢!
public async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
{
var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
return await blockBlob.ExistsAsync();
}
private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
{
return CloudStorageAccount
.Parse(connectionString)
.CreateCloudBlobClient()
.GetContainerReference(containerName)
.GetBlockBlobReference(fileName);
}
请尝试更新WindowsAzure.Storage
到最新版本v9.3.3。
我使用您的代码进行测试,并且 blob 名称中没有包含空格的问题。
示例代码:
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System;
using System.Threading.Tasks;
namespace AzureBlobConsole
{
class Program
{
static void Main(string[] args)
{
string conn = "xxxx";
bool x = CheckExistsAsync(conn, "f11", "222 json test.json").GetAwaiter().GetResult();
//to see if the file exists or not
Console.WriteLine(x);
Console.WriteLine("completed.");
Console.ReadLine();
}
public static async Task<bool> CheckExistsAsync(string connectionString, string containerName, string fileName)
{
var blockBlob = GetBlockBlobReference(connectionString, containerName, fileName);
return await blockBlob.ExistsAsync();
}
private static CloudBlockBlob GetBlockBlobReference(string connectionString, string containerName, string fileName)
{
return CloudStorageAccount
.Parse(connectionString)
.CreateCloudBlobClient()
.GetContainerReference(containerName)
.GetBlockBlobReference(fileName);
}
}
}
测试结果:
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句