我尝试在服务总线主题触发 Azure 函数中使用 blob url 下载 blob,但出现以下错误:
Status: 404 (The specified blob does not exist.)
ErrorCode: BlobNotFound
Content:
<?xml version="1.0" encoding="utf-8"?><Error><Code>BlobNotFound</Code><Message>The specified blob does not exist.
</Message></Error>
下面是我的 Startup.cs 文件:
[assembly: FunctionsStartup(typeof(Startup))]
namespace Emails.Dispatcher
{
public class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
builder.Services.AddSingleton(x =>
new BlobServiceClient(connectionString: Environment.GetEnvironmentVariable("AzureWebJobsStorage")));
builder.Services.AddSingleton<IBlobService, BlobService>();
builder.Services.AddLogging();
builder.Services.AddHttpClient();
builder.AddHelpers();
builder.Services.AddWorkers();
}
}
}
Blob 服务:
public class BlobService : IBlobService
{
private readonly BlobServiceClient _blobServiceClient;
public BlobService(BlobServiceClient blobServiceClient)
{
this._blobServiceClient = blobServiceClient;
}
public async Task<byte[]> DownloadAsync(string containerName, string fileUri)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(fileUri);
using (var ms = new MemoryStream())
{
await blobClient.DownloadStreamingAsync();
return ms.ToArray();
}
}
public async Task<BlobDto> UploadAsync(string containerName, string filename, string content)
{
var containerClient = _blobServiceClient.GetBlobContainerClient(containerName);
var blobClient = containerClient.GetBlobClient(filename);
var bytes = Encoding.UTF8.GetBytes(content);
await using var memoryStream = new MemoryStream(bytes);
await blobClient.UploadAsync(content);
return new BlobDto
{
Uri = blobClient.Uri,
Name = blobClient.Name
};
}
}
public interface IBlobService
{
Task<BlobDto> UploadAsync(string containerName, string filename, string content);
Task<byte[]> DownloadAsync(string containerName, string fileUri);
}
实际上,此服务正在接收完整的 blob url (即 https://{storage-url}.blob.core.windows.net/{container-name}/files/unzip/691ec307-7c2b-4aaa- 8f75-8742c9faa9f5/1615015726/{文件名}.pdf)。
我在这里错过了什么吗?
最佳答案
问题出在以下代码行:
var blobClient = containerClient.GetBlobClient(fileUri);
当您使用 ContainerClient
和 GetBlobClient
创建 BlobClient
时,您只需提供 blob 的名称,而不是整个 URL。
要解决此问题,只需传递 blob 名称即可。
关于c# - 使用 Uri 下载 blob 时出现错误 "404 The specified blob does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73337005/