c# - 使用 Uri 下载 blob 时出现错误 "404 The specified blob does not exist"

标签 c# azure dependency-injection azure-functions azure-blob-storage

我尝试在服务总线主题触发 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);

当您使用 ContainerClientGetBlobClient 创建 BlobClient 时,您只需提供 blob 的名称,而不是整个 URL。

要解决此问题,只需传递 blob 名称即可。

关于c# - 使用 Uri 下载 blob 时出现错误 "404 The specified blob does not exist",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73337005/

相关文章:

azure - Service Now 与 Azure DevOps 集成

.net - Ninject 运行时上下文绑定(bind)

c# - 如何根据当前键盘布局将虚拟键码转换为字符?

c# - 如何将属性分配给属性

c# - 人们如何将大量数据 (ETL) 从 ASP.NET 网站导入数据库?

android - 了解 Dagger 2 中的作用域

dependency-injection - 我可以在 CaSTLe Windsor 中为代理类型定义自定义属性吗

c# - 如何使用 Roslyn 将 C# 关键字替换为字符串替代项?

c# - 字符串 "de-concatenation"

.net - 跨 azure serviceFabric 服务共享文件