【问题标题】:Azure Blob Storage: Download all logs from Azure Storage Container "$logs" using C#Azure Blob 存储:使用 C# 从 Azure 存储容器“$logs”下载所有日志
【发布时间】:2020-10-13 12:32:57
【问题描述】:

我正在尝试从容器“$logs”下载所有日志,但它总是抛出异常 -

“找不到路径'C:\logs\blob\2020\05\24\2300\000000.log'的一部分”

public static void GetAnalyticsLogs(CloudBlobClient blobClient, CloudTableClient tableClient)
{
    try
    {
        DateTime time = DateTime.UtcNow;
        CloudAnalyticsClient analyticsClient = new CloudAnalyticsClient(blobClient.StorageUri, tableClient.StorageUri, tableClient.Credentials);
        IEnumerable<ICloudBlob> results = analyticsClient.ListLogs(StorageService.Blob, time.AddDays(-30), null, LoggingOperations.All, BlobListingDetails.Metadata, null, null);
        List<ICloudBlob> logs = results.ToList();
        foreach (var item in logs)
        {
            string name = ((CloudBlockBlob)item).Name;
            CloudBlobContainer container = blobClient.GetContainerReference("$logs");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
            string path = (@"C:/logs/" + name);
            using (var fileStream = System.IO.File.Create(path))
            {
                blockBlob.DownloadToStream(fileStream);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

我们如何解决这个错误?

【问题讨论】:

标签: azure azure-storage azure-blob-storage


【解决方案1】:

原因是路径包含目录,但如果目录不存在,File.Create() 方法无法在目录内创建文件。所以你应该先创建目录,然后使用 File.Create() 方法创建文件。

下面的代码在我这边运行良好:

public static void GetAnalyticsLogs(CloudBlobClient blobClient, CloudTableClient tableClient)
{
    try
    {
        DateTime time = DateTime.UtcNow;
        CloudAnalyticsClient analyticsClient = new CloudAnalyticsClient(blobClient.StorageUri, tableClient.StorageUri, tableClient.Credentials);
        IEnumerable<ICloudBlob> results = analyticsClient.ListLogs(StorageService.Blob, time.AddDays(-30), null, LoggingOperations.All, BlobListingDetails.Metadata, null, null);
        List<ICloudBlob> logs = results.ToList();
        foreach (var item in logs)
        {
            string name = ((CloudBlockBlob)item).Name;
            CloudBlobContainer container = blobClient.GetContainerReference("$logs");
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);

            //specify the directory without file name
            string sub_folder = name.Remove(name.LastIndexOf("/") + 1);                   
            string path = (@"C:/logs/" + sub_folder);

            //create the directory if it does not exist.
            Directory.CreateDirectory(path);

            //specify the file full path
            string file_path= (@"C:/logs/" + name);

            using (var fileStream = File.Create(file_path))
            {
                
                blockBlob.DownloadToStream(fileStream);
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

【讨论】:

    猜你喜欢
    • 2016-09-01
    • 2013-01-21
    • 2015-04-24
    • 2015-08-22
    • 2012-12-08
    • 2020-10-05
    • 1970-01-01
    • 2015-06-20
    • 2022-11-02
    相关资源
    最近更新 更多