如何序列化.NET对象到Azure的Blob存储,而无需使用临时文件? [英] How do I serialize a .NET object into Azure Blob Storage without using a temporary file?

查看:208
本文介绍了如何序列化.NET对象到Azure的Blob存储,而无需使用临时文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要存储一个.NET对象到Azure的Blob存储。

I want to store a .NET object into Azure Blob Storage.

目前我序列化到使用XML文件的TextWriter episodeList 是我想要序列化对象):

Currently I serialize it into an XML file using TextWriter (episodeList is the object I want serialized):

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes Xmlattr = new XmlAttributes();
Xmlattr.XmlRoot = new XmlRootAttribute("EPISODES");
overrides.Add(typeof(List<EpisodeData>), Xmlattr);
XmlSerializer serializer = new XmlSerializer(typeof(List<EpisodeData>), overrides);
TextWriter textWriter = new StreamWriter(@"C:\movie.xml");
serializer.Serialize(textWriter, episodeList);
textWriter.Close();

,然后上传文件到Blob存储:

and then upload the file into Blob Storage:

CloudBlobClient blobStorage = createOrGetReferenceOfBlobStorage(folderName);
string uniqueBlobName = string.Format("{0}/{1}", folderName, fileName);
CloudBlockBlob blob = clouBblockBlobPropertySetting(blobStorage, uniqueBlobName, ".txt");
using (StreamWriter writer = new StreamWriter(blob.OpenWrite()))
{
    writer.Write(content);
} 

是否有可能以某种方式跳过该临时文件,以便XML被直接上传到Azure的Blob存储?

Is it possible to somehow skip the temporary file so that the XML is directly uploaded into Azure Blob Storage?

推荐答案

您可以执行以下操作。创建一个的MemoryStream 实例,并使用 XmlSerializer.Serialize(流流)的对象序列化到内存流,那么倒带使用要寻求流开始()。然后调用 CloudBlob.UploadFromStream()流中的内容上传到BLOB。

You could do the following. Create a MemoryStream instance and use XmlSerializer.Serialize(Stream stream) to serialize the object into the memory stream, then "rewind" the stream to beginning using Seek(). Then you call CloudBlob.UploadFromStream() to upload the stream contents to the blob.

这篇关于如何序列化.NET对象到Azure的Blob存储,而无需使用临时文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆