【问题标题】:How to get files modified today from Azure Blob Storage如何从 Azure Blob 存储中获取今天修改的文件
【发布时间】:2019-04-06 11:06:26
【问题描述】:

我想从 Azure Blob 存储中获取每天都会修改的文件。我们在 Azure 中有一个容器,其中每天填充两个 Excel 文件,我需要获取这些文件。

到目前为止,我只能使用latestmodifiedon 获取一个文件。如何获取这两个文件?

private static DataSet GetExcelBlobData()
{
    var containerName = "salesbycontract";
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(Microsoft.Azure.CloudConfigurationManager.GetSetting("StorageConnectionString"));

    CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    CloudBlobContainer container = blobClient.GetContainerReference(containerName);
    CloudBlockBlob blockbob = container.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(m => m.Properties.LastModified).ToList().First();

    var x = blockbob.Name;
    Console.WriteLine(x);

    DataSet ds;
    using (var memstream = new MemoryStream())
    {
        blockbob.DownloadToStream(memstream);

        var excelReader = ExcelReaderFactory.CreateOpenXmlReader(memstream);
        ds = excelReader.AsDataSet();
        excelReader.Close();
    }
    return ds;
}

【问题讨论】:

    标签: c# azure azure-blob-storage


    【解决方案1】:

    只需添加一个Where 子句并与DateTime.Today 进行比较:

    var blockbob = container.ListBlobs().OfType<CloudBlockBlob>()
        .Where(m => m.Properties.LastModified.Value.Date == DateTime.Today).ToList().First();
    

    我添加了一个working example to my GitHub 存储库,它使用 dotnet 核心和最新的 WindowsAzure.Storage SDK:

    public async Task RetrieveBlobsModifiedTodayAsync()
    {
        var container = _blobClient.GetContainerReference(_storageAccount.ContainerName);
    
        BlobContinuationToken blobContinuationToken = null;
        do
        {
            var results = await container.ListBlobsSegmentedAsync(null, blobContinuationToken);
    
            var blobs = results.Results.OfType<CloudBlockBlob>()
                .Where(b => b.Properties.LastModified != null && b.Properties.LastModified.Value.Date == DateTime.Today);
    
            blobContinuationToken = results.ContinuationToken;
            foreach (var item in blobs)
            {
                Console.WriteLine(item.Uri);
            }
        } while (blobContinuationToken != null); // Loop while the continuation token is not null. 
    }
    

    【讨论】:

    • 嗨马丁,关于您的查询,如何使用 excel 数据阅读器读取文件,并返回到列表数据集?
    • 请考虑使用绿色复选标记接受答案并开始一个新问题。同时还原您对问题所做的更改。
    • 不幸的是,这执行过滤客户端,而不是服务器端。就我而言,我有一个 Azure blob 容器,其中包含 400 万个 blob(使用内容可寻址存储方案,因此每个 blob 的名称都是它的哈希) - 一次获取所有 blob 的列表大约需要 30 分钟。如果 Azure 可以对 blob 进行服务器端过滤,我会很高兴。
    【解决方案2】:

    从 Fikri Hailal's (OP) 迁移到 2018 年 11 月 8 日 edit 为他们的问题添加了答案:

    以下是适用于我的情况的查询:

    var blockbob = container.ListBlobs().OfType<CloudBlockBlob>().OrderByDescending(m => m.Properties.LastModified).ToList().Take(2);
    

    【讨论】:

    • 这涉及到一个ListBlobs(),它不会返回所有文件吗?如果我们在 blob 中有数百万个文件怎么办?
    猜你喜欢
    • 2023-01-16
    • 2016-07-15
    • 2012-06-16
    • 2021-11-01
    • 2017-09-06
    • 2015-11-04
    • 2021-10-18
    • 1970-01-01
    • 2021-06-13
    相关资源
    最近更新 更多