c# - 仅将更新的文件上传到 Blob 存储

标签 c# azure azure-functions azure-blob-storage

我有一种方法可以将 XML 文件从文件夹上传到 Blob 存储中。连接到该 Blob 存储时,我有一个 Blob 触发器,用于监听 Blob 存储中的更改、获取文件,然后向服务器发出 PUT 请求。我已经解决了并开始工作。

我的问题是,当我想要更新文件夹中的特定文件并运行代码时,文件夹中的所有文件似乎都会再次上传,并且我的 Blob 触发器会停止,对所有文件执行 PUT。我只想对文件夹中更改的文件执行 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";
    }

执行 PUT 操作的 Blob 触发器:

 public static class BlobTriggerExample
{
    const string serverUrl= "theurl";
    static HttpClient client = new HttpClient();

    [FunctionName("BlobTriggerExample")]
    public static async Task Run([BlobTrigger("myblob/{name}", Connection = "AzureWebJobsStorage")]CloudBlockBlob myBlob, string name, TraceWriter log)
    {          
        string putUrlString = "";
        string idValue = "";
        XDocument xdoc = new XDocument();
        myBlob.StreamMinimumReadSizeInBytes = 20 * 1024 * 1024;
        await myBlob.FetchAttributesAsync();
        //Read stream
        var blobStream = await myBlob.OpenReadAsync();

            xdoc = new XDocument(XDocument.Load(blobStream));

        //Read root node(resourceType)
        string resourceType = xdoc.Root.Name.LocalName;

        //Get id value
        idValue = xdoc.Descendants().Where(x => x.Name.LocalName == "id").First().LastAttribute.Value;

        //Build redirect string
        putUrlString = serverUrl + resourceType + "/" + idValue;

        //PUT
        var httpContent = new StringContent(xdoc.ToString(), Encoding.UTF8, "application/xml");
        var response = await client.PutAsync(putUrlString, httpContent);    

        Console.WriteLine($"Response: {response}");
        Console.Read();

        log.Info($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.StreamWriteSizeInBytes} Bytes : Response message: {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 并使用它进行比较 - 请参阅 https://learn.microsoft.com/en-us/azure/storage/blobs/storage-properties-metadata .

关于c# - 仅将更新的文件上传到 Blob 存储,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52605960/

相关文章:

Azure Function 插槽已预热,但仍经历冷启动

azure - 通过ARM模板创建.NET 5 Function App后为"Service Unavailable"

c# - asp.net 到 MySql 的问题。尝试从输入返回信息

c# - 转换 C++ 中的 std::string,使其成为 C# 中的 byte[]

c# - .NET 4.0 与 2.0 中的数据绑定(bind)工作方式不同

c# - 为什么 Visual Studio 在使用 "Comment Selection"注释多行选择时求助于单行注释?

azure - 如何使用 Azure DataFactory 解压缩 rar 文件

azure - 使用 Visual Studio Code 在本地运行和调试 2 Azure 函数

c# - 事件中心错误 : Put token failed. 状态代码 : 401, 状态描述:ExpiredToken

azure - 如何使用颁发者证书在 Azure key 保管库中签署 CSR