gpt4 book ai didi

c# - 如何从 Azure Blob 存储获取当前修改的文件

转载 作者:行者123 更新时间:2023-11-30 16:39:26 25 4
gpt4 key购买 nike

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

最佳答案

只需添加 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 core 和最新的 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.
}

关于c# - 如何从 Azure Blob 存储获取当前修改的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53114584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com