如何在Azure Blob存储上下载名称上带有空格的文件?

时间:2017-07-12 14:17:00

标签: azure azure-blob-storage

我正在尝试从此网址下载文件:

https://renatoleite.blob.core.windows.net/mycontainer/documents/Test Document.pdf

浏览器正在将URL更改为:

https://renatoleite.blob.core.windows.net/mycontainer/documents/Test%20Document.pdf

blob存储中的文件名为:Test Document.pdf

因此,当我点击下载时,Azure说该文件不存在:

  

指定的资源不存在。

可能是因为浏览器试图在名称中使用“%20”来获取文件。

我如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

据我所知,如果您想使用azure storage api上传文件空间名称,它会在上传时自动编码名称(用%20替换空格)。

您可以在下面看到示例:

我将Test Document.pdf上传到blob存储区。

        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve a reference to a container.
        CloudBlobContainer container = blobClient.GetContainerReference("brando");

        // Create the container if it doesn't already exist.
        container.CreateIfNotExists();

        // Retrieve reference to a blob named "myblob".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference("Test Document.pdf");

        // Create or overwrite the "myblob" blob with contents from a local file.
        using (var fileStream = System.IO.File.OpenRead(@"D:\Test Document.pdf"))
        {
            blockBlob.UploadFromStream(fileStream);
        }

然后我建议您可以使用storage explorer(右键单击属性查看其URL)或azure portal来查看blob属性中的URL。

这样的网址:

您可以找到用%20替换空格。

enter image description here