如何解决如何在 dotnet core 的块 blob 类型存储中的现有文件中追加新的 csv 数据我尝试追加,但显示错误
这是我的代码:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(AzureWebJobsStorage);
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference(ContainerName);
// CloudBlobContainer containerbackup = serviceClient.GetContainerReference(ContainerNameBackup);
CloudBlockBlob blob = container.GetBlockBlobReference(FileName);
// CloudBlockBlob blobBackUp = containerbackup.GetBlockBlobReference(FileName);
// CloudAppendBlob appendBlob = container.GetAppendBlobReference(FileName);
byte[] csvFile = Encoding.UTF8.GetBytes(csveventcontent.ToString().Trim());
if ( await blob.ExistsAsync())
using (var appendFileStream = new MemoryStream(csvFile))
await blob.UploadFromStreamAsync(appendFileStream);
else
{
using (var uploadFileStream = new MemoryStream(csvFile))
{
await blob.UploadFromStreamAsync(uploadFileStream);
uploadFileStream.Close();
}
}
解决方法
没有直接追加数据的方法。 UploadFromStreamAsync
用于将流上传到块 blob。如果 blob 已经存在,它将被覆盖。
您可以将 blob 文件作为流下载,然后附加 csv 数据流,最后上传新的流。
// download the blob stream:
MemoryStream stream = new MemoryStream();
await blob.DownloadToStreamAsync(stream);
stream.Position = 0;
// append the stream,refer to https://stackoverflow.com/questions/15655210/how-to-merge-two-memory-streams
var newStream = Append(stream,appendFileStream);
// upload the new stream
await blob.UploadFromStreamAsync(newStream);
附加流方法:
public static Stream Append(Stream destination,Stream source)
{
destination.Position = destination.Length;
source.CopyTo(destination);
destination.Position = 0;
return destination;
}
我的测试代码:
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
var StorageConnectionString = "";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(StorageConnectionString);
CloudBlobClient serviceClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = serviceClient.GetContainerReference("pamelac");
CloudBlockBlob blob = container.GetBlockBlobReference("test.txt");
var appendFileStream = GetStreamFromString("hello"); // just for test,it will be "var appendFileStream = new MemoryStream(csvFile);" in your case
// download the blob stream:
MemoryStream stream = new MemoryStream();
await blob.DownloadToStreamAsync(stream);
stream.Position = 0;
Console.WriteLine("download stream to string: \n" + Encoding.UTF8.GetString((stream as MemoryStream).ToArray()));
// append the stream
var newStream = Append(stream,appendFileStream);
Console.WriteLine("new stream to string: \n" + Encoding.UTF8.GetString((newStream as MemoryStream).ToArray()));
// upload the new stream
await blob.UploadFromStreamAsync(newStream);
}
public static Stream GetStreamFromString(string text)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(text);
writer.Flush();
stream.Position = 0;
return stream;
}
public static Stream Append(Stream destination,Stream source)
{
destination.Position = destination.Length;
source.CopyTo(destination);
destination.Position = 0;
return destination;
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。