【问题标题】:Unable to create container with a forward slash in Azure无法在 Azure 中创建带有正斜杠的容器
【发布时间】:2017-01-28 20:41:21
【问题描述】:

我无法使用以下代码在 Azure 中创建包含正斜杠(我也尝试过反斜杠)的容器。创建一个没有斜线的容器是可行的。

容器名称符合 azure 规则。它介于容器名称的最小和最大长度之间,是小写字母等等。我收到来自 Azure '400: bad request' 的一般响应

var exampleDirectory = "example/directory";
var cloudStorage = new CloudStorageAndDBConnections();
var blobClient = cloudStorage.blobClient;
var exampleContainer = blobClient.GetContainerReference(exampleDirectory ).CreateIfNotExists();

用“exampledirectory”而不是“example/directory”来尝试这个是可行的。

我已阅读说明允许使用正斜杠的文档,所以我迷路了..

感谢您的帮助。

【问题讨论】:

    标签: c# azure


    【解决方案1】:

    您的容器名称无效,因为容器名称可能不包含斜杠 (/) 字符。仅允许在 blob 名称中使用斜杠。具体来说,对于容器名称(根据规则,here):

    • 容器名称必须以字母或数字开头,只能包含字母、数字和短划线 (-) 字符。

    • 每个破折号 (-) 字符的前后必须紧跟一个字母或数字;容器名称中不允许使用连续的破折号。

    • 容器名称中的所有字母都必须小写。

    • 容器名称的长度必须为 3 到 63 个字符。

    【讨论】:

    • 啊,谢谢 - 我想我一定是把容器和 blob 搞混了。
    【解决方案2】:

    这个答案假设这是建立 blob 的 url。

    这是设计使然。容器名称将始终位于任何 url 的根目录。

    您不需要填写正斜杠 - azure 会为您完成。因此,如果您的容器有以下 blob:

    • exampleblob.txt
    • examplesub/exampleblob.txt
    • examplesub/examplesub/exampleblob.txt

    如果您的容器名称是 exampledirectory,它们将解析为以下 url:

    • exampleblobservice.blob.core.windows.net/exampledirectory/exampleblob.txt
    • exampleblobservice.blob.core.windows.net/exampledirectory/examplesub/exampleblob.txt
    • exampleblobservice.blob.core.windows.net/exampledirectory/examplesub/examplesub/exampleblob.txt

    所以 blob 服务会自动填写正斜杠

    【讨论】:

      【解决方案3】:

      Azure 存储中不存在“目录”的概念。您创建一个容器并在那里上传文件。

      如果要创建“目录”结构,请将“路径”添加到文件名中。它会自动生成,因为您存储的是文件和路径。

      然后您可以使用您创建的 url 结构请求文件。

      var exampleDirectory = "example";
      var cloudStorage = new CloudStorageAndDBConnections();
      var blobClient = cloudStorage.blobClient;
      var exampleContainer = blobClient.GetContainerReference(exampleDirectory ).CreateIfNotExists();
      var fileBase = exampleContainer.GetBlockBlobReference("directory/myfile.extension");
      await fileBase.UploadFromStreamAsync(stream);
      var myUrlWithForwardSlashes = fileBase.Uri.ToString();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-09-09
        • 2012-11-16
        • 1970-01-01
        • 2020-02-28
        • 2021-02-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多