如何附加到Azure存储文件共享中的文件? [英] How do I append to a file in an Azure storage file share?
问题描述
我想将条目写入存储在Azure文件存储中的日志文件中。我目前有这个:
I want to write entries to a log file stored in Azure file storage. I currently have this:
var log = "My log entry";
var client = _storageAccount.CreateCloudFileClient();
var share = client.GetShareReference(Config.LogShare);
share.CreateIfNotExists();
var root = share.GetRootDirectoryReference();
var logfile = root.GetFileReference("log.txt");
if (!logfile.Exists()) logfile.Create(0);
// What goes here to append to the file...?
我可以看到很多有关如何使用Blob执行此操作或如何上传整个文件的示例,但是如何附加到现有文件呢?
I can see plenty of examples of how to do this with Blobs, or how to upload an entire file, but how do I just append to an existing file?
我已经尝试过:
var buffer = Encoding.GetEncoding("UTF-8").GetBytes(log.ToCharArray());
using (var fileStream = logfile.OpenWrite(0)) {
fileStream.Write(buffer, (int)logfile.Properties.Length, buffer.Length);
}
但随后出现此错误:
The remote server returned an error: (416) The range specified is invalid for the current size of the resource..
推荐答案
Azure文件存储REST API不支持附加到现有文件。为此,请将文件共享作为驱动器安装到计算机上,然后像简单的本地文件一样附加到文件中。
Azure file storage REST API doesn't support appending to an existing file. To achieve this, please mount the file share to your machine as a drive, and append to the file just like simple local files.
实际上,我不认为您确实需要根据上面的代码附加功能。您可以在CloudFile.OpenWrite()/ CloudFile.Create()中指定文件大小,或尝试 CloudFile.UploadFromStream()而不是CloudFile.OpenWrite()。
Actually, I don't think you really need appending functionality per your code above. You can specify the file size in CloudFile.OpenWrite() / CloudFile.Create(), or try CloudFile.UploadFromStream() instead of CloudFile.OpenWrite().
这篇关于如何附加到Azure存储文件共享中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!