简体   繁体   English

Azure Blob存储-当我保存到文件时为404

[英]Azure Blob Storage - 404 when i save to file

I have another problem with Azure Blob Storage, this time with downloading. 我有Azure Blob存储的另一个问题,这次是下载。 I get a list of files without a problem, unfortunately when I want to download it I get a 404 error that the file was not found. 我得到了没有问题的文件列表,不幸的是,当我要下载它时,出现了404错误,找不到该文件。

using System.IO;
using System.Linq;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

namespace BlobStorage
{
    class Program
    {
        static void Main(string[] args)
        {
            CloudStorageAccount backupStorageAccount = CloudStorageAccount.Parse(
                "{connectionString}");

            var backupBlobClient = backupStorageAccount.CreateCloudBlobClient();
            var backupContainer = backupBlobClient.GetContainerReference("{container-name");

            var list = backupContainer.ListBlobs(useFlatBlobListing: true);

            foreach (var blob in list)
            {
                var blobFileName = blob.Uri.Segments.Last();
                CloudBlockBlob blockBlob = backupContainer.GetBlockBlobReference(blobFileName);

                string destinationPath = string.Format(@"D:\" + blobFileName +".txt");

                blockBlob.DownloadToFile(destinationPath, FileMode.OpenOrCreate);
            }
        }
    }
}

Error message: 错误信息:

Microsoft.WindowsAzure.Storage.StorageException: "The remote server returned an error: (404) Not found." Microsoft.WindowsAzure.Storage.StorageException:“远程服务器返回错误:(404)未找到。”

Internal exception WebException: The remote server returned an error: (404) Not found. 内部异常WebException:远程服务器返回错误:(404)找不到。

And points to the line: 并指向该行:

blockBlob.DownloadToFile (destinationPath, FileMode.OpenOrCreate);

A file like this most exists in blob storage. 此类文件最多存在于Blob存储中。 When I enter the blob editions, copy the url to a file, I can download it through the browser without any problem. 当我输入Blob版本时,将url复制到文件中,我可以通过浏览器下载它而没有任何问题。 Unfortunately, I can not download it from the application level due to a 404 error. 不幸的是,由于404错误,我无法从应用程序级别下载它。

Only why does such a file exist? 只有这样的文件为什么存在?

The issue is how you're getting the blob name in the following line of code: 问题是您如何在以下代码行中获取Blob名称:

var blobFileName = blob.Uri.Segments.Last();

Considering, the path is tempdata/ExampleIotHub/02/2019/05/14/39 , the blob's name is ExampleIotHub/02/2019/05/14/39 (assuming your container name is tempdata ) however the blobFileName you're getting is just 39 (please see examples here ). 综合考虑,路径是tempdata/ExampleIotHub/02/2019/05/14/39 ,斑的名字是ExampleIotHub/02/2019/05/14/39 (假设你的容器名称是tempdata ),但是你得到的blobFileName是仅39(请here查看示例)。 Since there is no blob by the name 39 , you're getting this 404 error. 由于没有名称为39 blob,因此会出现此404错误。

I suggest you try by doing something like the following: 我建议您尝试执行以下操作:

foreach (var blob in list)
{
    var localFileName = blob.Uri.Segments.Last();
    CloudBlockBlob blockBlob = blob as CloudBlockBlob;
    if (blockBlob != null)
    {
      string destinationPath = string.Format(@"D:\" + localFileName +".txt");

      blockBlob.DownloadToFile(destinationPath, FileMode.OpenOrCreate);
    }
}

Please note that I have not tried running this code so there may be some errors. 请注意,我没有尝试运行此代码,因此可能会出现一些错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM