C# 仅将更新的文件上载到Blob存储中

C# 仅将更新的文件上载到Blob存储中,c#,azure,azure-functions,azure-blob-storage,C#,Azure,Azure Functions,Azure Blob Storage,我有一种方法可以将XML文件从文件夹上传到Blob存储中。连接到Blob存储,我有一个Blob触发器,它监听Blob存储中的更改,获取文件,然后向服务器发出PUT请求。我把它整理好并开始工作 我的问题是,当我想更新文件夹中的一个特定文件并运行代码时,文件夹中的所有文件似乎都会再次上载,而我的Blob触发器会触发,对所有文件执行PUT操作。我只想对文件夹中更改的文件进行一次放置(当然,除了我最初上传到blob之外) 到目前为止,我掌握的代码与我的经验水平一样基本。对于导入,我遵循了一个简单的指南

我有一种方法可以将XML文件从文件夹上传到Blob存储中。连接到Blob存储,我有一个Blob触发器,它监听Blob存储中的更改,获取文件,然后向服务器发出PUT请求。我把它整理好并开始工作

我的问题是,当我想更新文件夹中的一个特定文件并运行代码时,文件夹中的所有文件似乎都会再次上载,而我的Blob触发器会触发,对所有文件执行PUT操作。我只想对文件夹中更改的文件进行一次放置(当然,除了我最初上传到blob之外)

到目前为止,我掌握的代码与我的经验水平一样基本。对于导入,我遵循了一个简单的指南

将文件上载到Blob存储的我的代码:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Starting...");
        string accountName = ConfigurationManager.AppSettings["accountName"];
        string accountKey = ConfigurationManager.AppSettings["accountKey"];
        string localFolder = ConfigurationManager.AppSettings["mySourceFolder"];
        string destContainer = ConfigurationManager.AppSettings["destContainer"];

        var stringReturned = BlobSetup(accountName, accountKey, localFolder, destContainer);

        Console.WriteLine(stringReturned);
        Console.Read();

    }
    static async Task UploadBlob(CloudBlobContainer container, string key, string filePath, bool deleteAfter)
    {
        //Get a blob reference to write this file to
        var blob = container.GetBlockBlobReference(key);

        await blob.UploadFromFileAsync(filePath);           

        Console.WriteLine("Uploaded {0}", filePath);
        //if delete of file is requested, do that
        if (deleteAfter)
        {
            File.Delete(filePath);
        }
    }
    static async Task<string> BlobSetup(string accountName, string accountKey, string localFolder, string destContainer)
     {
        var storageAccount = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
        var blobClient = storageAccount.CreateCloudBlobClient();

        var container = blobClient.GetContainerReference(destContainer);
        //create container if not exists
        await container.CreateIfNotExistsAsync();
        await container.SetPermissionsAsync(new BlobContainerPermissions()
        {
            PublicAccess = BlobContainerPublicAccessType.Blob
        });


        string[] fileEntries = Directory.GetFiles(localFolder);        
        foreach (string filePath in fileEntries)
        {
            //Handle only json and xml? 
            if(filePath.EndsWith(".json") || filePath.EndsWith(".xml"))
            {                  
                string keys = Path.GetFileName(filePath);

                await UploadBlob(container, keys, filePath, false);
            }

        }
        return "some response";
    }
我的猜测是,我希望能够通过检查是否存在完全相同的文件来控制我正在上传到Blob存储中的文件。或者我想在执行PUT之前检查一下Blob触发器

我上传的文件夹中的文件名总是相同的(这是必须的),即使有些内容可能已经更改了


有没有人能如此好心地给我一些指导,告诉我如何处理这个问题?我在谷歌上搜索了几个小时,结果一无所获。

是的,你的代码循环并上传了你本地文件夹中的所有文件。blob触发器只看到blob已被写入,不知道它们的内容是否已更改(或是否发生了更改),因此它也会处理所有blob

您需要做的是在上载之前将本地文件与blob存储中的文件进行比较,以确定它们是否为新版本,因此在
UploadBlob
方法中,您需要与以下内容类似的内容:

// Get a blob reference to write this file to
var blob = container.GetBlockBlobReference(key);
// If the blob already exists
if (await blob.ExistsAsync()) {
    // Fetch the blob's properties
    await blob.FetchAttributesAsync();
    // Only proceed if modification time of local file is newer
    if (blob.Properties.LastModified > File.GetLastWriteTimeUtc(filePath))
        return;
}

如果检查修改时间不够,那么您也可以将自己的元数据(例如校验和)附加到blob,并将其用于比较-请参阅。

非常感谢。效果很好。
// Get a blob reference to write this file to
var blob = container.GetBlockBlobReference(key);
// If the blob already exists
if (await blob.ExistsAsync()) {
    // Fetch the blob's properties
    await blob.FetchAttributesAsync();
    // Only proceed if modification time of local file is newer
    if (blob.Properties.LastModified > File.GetLastWriteTimeUtc(filePath))
        return;
}