如何今天从Azure Blob存储获取文件修改

时间:2018-11-02 07:48:57

标签: c# azure azure-blob-storage

我想从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;
}

2 个答案:

答案 0 :(得分:1)

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

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

我添加了一个{.3}}存储库,该存储库使用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. 
}

答案 1 :(得分:0)

Fikri Hailal's (OP)迁移至edit,并为他们的问题添加了答案:

  

下面是适合我的情况的查询:

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