【问题标题】:Azure.Storage.Blobs Folder StructureAzure.Storage.Blobs 文件夹结构
【发布时间】:2021-04-11 10:11:42
【问题描述】:

几年来,我们已经使用已折旧的 WindowsAzure.Storage nuget 在 Azure 上进行了现有实施。

我们正在将 nuget 升级到新的 Azure.Storage.Blobs nuget。

作为原始实现的一部分,blob 将存储在容器内的文件夹结构中:container/year/month/:

  • test/2021/01/test.pdf
  • test/2020/12/test.pdf

要求这应该继续向前,但如果可以的话,我不能锻炼了。

有没有人设法让它工作?

【问题讨论】:

  • 不确定你的意思。文件夹是虚拟的。 Blob 名称定义了它们。
  • 这是正确的,在旧的 nuget 中它运行良好。但是,在最新版本中,它不再起作用,因为 blob 中的 url 被 url 编码,因此在创建文件时会丢失虚拟结构。

标签: c# azure azure-blob-storage


【解决方案1】:

在 Azure 的 Blob 存储中,您没有所谓的文件夹结构。都是虚拟的。如果您在代码的 blob 引用部分中使用您希望它的完整路径(在您的情况下为年/月)指定 blob 的名称,则可以保留与上传 blob 相同的逻辑。

【讨论】:

    【解决方案2】:

    您可以通过代码简单地创建它:

    using Azure.Storage;
    using Azure.Storage.Blobs;
    using Azure.Storage.Blobs.Models;
    
    // Get a connection string to our Azure Storage account.  You can
    // obtain your connection string from the Azure Portal (click
    // Access Keys under Settings in the Portal Storage account blade)
    // or using the Azure CLI with:
    //
    //     az storage account show-connection-string --name <account_name> --resource-group <resource_group>
    //
    // And you can provide the connection string to your application
    // using an environment variable.
    string connectionString = "<connection_string>";
    
    // Get a reference to a container named "sample-container" and then create it
    BlobContainerClient container = new BlobContainerClient(connectionString, "sample-container");
    container.Create();
    
    // Get a reference to a blob named "sample-file" in a container named "sample-container"
    BlobClient blob = container.GetBlobClient("sample-folder/sample-file");
    
    // Open a file and upload it's data
    using (FileStream file = File.OpenRead("local-file.jpg"))
    {
        blob.Upload(file);
    }
    

    查看documentation from PG 了解此 nuget。

    【讨论】:

    • 这个答案对你有帮助吗?
    • 你好,很遗憾没有。这是使用已被折旧的旧 nuget 包,这与我们之前的做法差不多。 nuget.org/packages/Azure.Storage.Blobs/12.0.0 是较新的版本。
    • 它具有相同的方法。请检查我的更新回复。
    • 对你有帮助吗?
    • 感谢您的更新。运行上面的代码给了我类似的行为。使用名称中的文件夹路径创建客户端,名称设置正确。但是,内部生成的 uri url 对名称进行编码,因此,我没有得到文件夹,只是一个不正确的文件名。例如:container.GetBlobClient("2020/01/90afae7a-de06-4239-8c1b-a01e5e62f199.png"); 返回一个 blob,其属性如下所示:Name: "2021/01/90afae7a-de06-4239-8c1b-a01e5e62f199.png" Uri: "/development/2021%2F01%2F90afae7a-de06-4239-8c1b-a01e5e62f199.png" (绝对路径)
    猜你喜欢
    • 2020-10-08
    • 2021-11-18
    • 2015-04-06
    • 2014-02-18
    • 2018-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多