无法引用文件名中包含空白 space 的块 blob
Cannot reference block blobs with blank space in filename
我正在使用 Azure SDK for .Net(版本 9.3.1,Platform .NET-Standard 2.0)处理 Azure Blob 存储,并且在引用 blob 中有空白 space 的块 blob 时遇到问题姓名。
我已经通过 Azure 存储资源管理器 1.6.1 将块 blob JSON Test.json 上传到私有容器中。
根据 Azure 存储资源管理器的 Blob 属性:
Name: `JSON Test.json`
URI: `https://<myaccountname>/<mycontainername>/JSON%20Test.json`
现在,我正在尝试使用 CloudBlockBlob.ExistsAsync()
方法检查该 Blob 是否存在传递给 GetBlockBlobReference
非编码文件名 JSON Test.json
结果为 FALSE。
现在,我在另一个容器中以编程方式创建一个 blob,传递相同的非编码文件名,使用相同的 GetBlockBlobReference
并创建一个具有编码文件名的 blob。
Name: `JSON%20Test.json`
URI: `https://<myaccountname>/<mycontainername2>/JSON%20Test.json`
我做错了什么?为什么在使用非编码文件名引用它时找不到通过 Azure 存储资源管理器创建的名称中带有空白 space 的块 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);
}
要检查 blob 是否存在,请尝试以下代码:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var found = await blobClient.GetBlobReferenceFromServerAsync(new Uri(filename));
您可能需要在最后一行通过 Uri 而不是字符串来访问文件。我还没有真正使用过你使用的异步方法,但上面的代码是适合我的代码。
请尝试更新WindowsAzure.Storage
到最新版本v9.3.3。
我使用你的代码进行测试,blob 名称包含白色没有问题 space。
示例代码:
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);
}
}
}
测试结果:
我正在使用 Azure SDK for .Net(版本 9.3.1,Platform .NET-Standard 2.0)处理 Azure Blob 存储,并且在引用 blob 中有空白 space 的块 blob 时遇到问题姓名。 我已经通过 Azure 存储资源管理器 1.6.1 将块 blob JSON Test.json 上传到私有容器中。
根据 Azure 存储资源管理器的 Blob 属性:
Name: `JSON Test.json`
URI: `https://<myaccountname>/<mycontainername>/JSON%20Test.json`
现在,我正在尝试使用 CloudBlockBlob.ExistsAsync()
方法检查该 Blob 是否存在传递给 GetBlockBlobReference
非编码文件名 JSON Test.json
结果为 FALSE。
现在,我在另一个容器中以编程方式创建一个 blob,传递相同的非编码文件名,使用相同的 GetBlockBlobReference
并创建一个具有编码文件名的 blob。
Name: `JSON%20Test.json`
URI: `https://<myaccountname>/<mycontainername2>/JSON%20Test.json`
我做错了什么?为什么在使用非编码文件名引用它时找不到通过 Azure 存储资源管理器创建的名称中带有空白 space 的块 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);
}
要检查 blob 是否存在,请尝试以下代码:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
var found = await blobClient.GetBlobReferenceFromServerAsync(new Uri(filename));
您可能需要在最后一行通过 Uri 而不是字符串来访问文件。我还没有真正使用过你使用的异步方法,但上面的代码是适合我的代码。
请尝试更新WindowsAzure.Storage
到最新版本v9.3.3。
我使用你的代码进行测试,blob 名称包含白色没有问题 space。
示例代码:
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);
}
}
}
测试结果: