How to Append a Text File in an Azure Blob with a Azure Function

谁说胖子不能爱 提交于 2021-02-09 20:33:58

问题


I've got a text file I need to append data to daily with a timer Azure Function. The text file is a comma separated file. I've created my CloudBlobClient and knew how to make my Shared Access Policy and Token. I just don't know how to use this to upload. I only know how to get an access URI from the tutorial I'm working with.


回答1:


I've got a text file I need to append data to daily with a timer Azure Function.

You can try to use append blob that is optimized for append operations. According to your description, you know how to get SAS URI, so you can use SAS URI to create a reference to an append blob, and append a file to an append blob, the following code is for your reference.

CloudAppendBlob appendBlob = new CloudAppendBlob(new Uri("https://{storage_account}.blob.core.windows.net/{your_container}/append-blob.log?st=2017-09-25T02%3A10%3A00Z&se=2017-09-27T02%3A10%3A00Z&sp=rwl&sv=2015-04-05&sr=b&sig=d0MENO44GjtBLf7L8U%2B%2F2nGwPAayjiVSSHaKJgEkmIs%3D"));


appendBlob.AppendFromFile("{filepath}\source.txt");



回答2:


    public class Blob
{
    public static async Task Save(string fileName, string message)
    {
        var blobContainer = ConfigurationManager.AppSettings["BlobContainer"];
        var blobConnectionString = ConfigurationManager.AppSettings["BlobConnectionString"];

        var storageAccount = CloudStorageAccount.Parse(blobConnectionString);
        var blobClient = storageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference(blobContainer);
        var cloudBlockBlob = container.GetBlockBlobReference(fileName);

        if (await cloudBlockBlob.ExistsAsync())
        {
            var oldContent = await cloudBlockBlob.DownloadTextAsync();
            var newContent = oldContent + "\n" + message;
            await cloudBlockBlob.UploadTextAsync(newContent);
        }
        else
        {
            await cloudBlockBlob.UploadTextAsync(message);
        }
    }
}


来源:https://stackoverflow.com/questions/46416808/how-to-append-a-text-file-in-an-azure-blob-with-a-azure-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!