将文本写入Azure存储中的文件

Spa*_*dan 1 azure azure-storage azure-storage-blobs

我在我的Azure存储account.Now上传文本文件中,在我的辅助角色,我需要做的是在每次运行时,它将从数据库中的一些内容,而且内容必须在上传文本文件写入,具体来说,每次文本文件的内容都应该被一些新内容覆盖.

在这里,他们给的方式到一个文本文件上传到您的存储空间,并且删除file.But我不想这样做,需要每次只需要修改已经存在的文本文件.

Dav*_*gon 6

I'm assuming you're referring to storing a file in a Windows Azure blob. If that's the case: A blob isn't a file system; it's just a place to store data (and the notion of a file is a bit artificial - it's just... a blob stored in a bunch of blocks).

To modify the file, you would need to download it and save it to local disk, modify it (again, on local disk), then do an upload. A few thoughts on this:

  • For this purpose, you should allocate a local disk within your worker role's configuration. This disk will be a logical disk, created on a local physical disk within the machine your vm is running on. In other words, it'll be attached storage and perfect for this type of use.
  • The bandwidth between your vm instance and storage is 100Mbps per core. So, grabbing a 10MB file, while on a Small instance, would take maybe a second. On an XL, maybe around a tenth of a second. really fast, and varies with VM series (A, D, G) and size.
  • Because your file is in blob storage, if you felt so inclined to do so (or had the need for this), you could take a snapshot prior to uploading an updated version. Snapshots are like link-lists to your stored data blocks. And there's no cost to snapshots until, one day, you make a change to existing data (and now you'd have blocks representing both old and new data). An excellent way to preserve versions of a blob on a blob-by-blob basis (and it's trivial to delete snapshots).

Just to make sure this download/modify/upload pattern is clear, here's a very simple example (I just typed this up quickly in Visual Studio but haven't tested it. Just trying to illustrate the point):

        // initial setup
        var acct = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        var client = acct.CreateCloudBlobClient();

        // what you'd call each time you need to update a file stored in a blob
        var blob = client.GetContainerReference("mycontainer").GetBlockBlobReference("myfile.txt");
        using (var fileStream = System.IO.File.OpenWrite(@"path\myfile.txt"))
        {
            blob.DownloadToStream(fileStream);
        }

        // ... modify file...

        // upload modified file
        using (var fileStream = System.IO.File.OpenWrite(@"path\myfile.txt"))
        {
            blob.UploadFromStream(fileStream);
        }
Run Code Online (Sandbox Code Playgroud)