简体   繁体   English

如何在不使用临时文件的情况下将.NET对象序列化为Azure Blob存储?

[英]How do I serialize a .NET object into Azure Blob Storage without using a temporary file?

I want to store a .NET object into Azure Blob Storage. 我想将.NET对象存储到Azure Blob存储中。

Currently I serialize it into an XML file using TextWriter ( episodeList is the object I want serialized): 目前我使用TextWriter将其序列化为XML文件( episodeList是我想要序列化的对象):

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();

and then upload the file into Blob Storage: 然后将文件上传到Blob存储:

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);
} 

Is it possible to somehow skip the temporary file so that the XML is directly uploaded into Azure Blob Storage? 有可能以某种方式跳过临时文件,以便将XML直接上载到Azure 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() . 创建一个MemoryStream实例并使用XmlSerializer.Serialize(Stream stream)将对象序列化到内存流中,然后使用Seek()将流“回放”到开头。 Then you call CloudBlob.UploadFromStream() to upload the stream contents to the blob. 然后调用CloudBlob.UploadFromStream()将流内容上传到blob。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Object c# .NET Core 在 blob 存储 azure 上创建 csv 文件? - How can I create a csv file on blob storage azure with Object c# .NET Core? 使用 Azure Blob 存储,如何在对 azure blob 文件发出请求后以自定义方式做出反应? - Using Azure Blob storage, how to I react in a custom way after a REQUEST TO an azure blob file? 如何使用 Azure.Storage.Blobs 程序集对 Azure blob 存储操作设置重试策略? - How do I set a retry policy on an Azure blob storage operation using the Azure.Storage.Blobs assembly? 将 Azure blob 存储与 .NET 结合使用 - Using Azure blob storage with .NET Azure Blob 存储 - 上传 Blob 后如何获取 Blob 存储 ID? - Azure Blob Storage - how do I get the Blob Storage ID after uploading a blob? 如何在不写入文件的情况下压缩 stream 并将其上传到 Azure Blob 存储? - How do you compress a stream and upload it to Azure Blob Storage without writing to a file? 如何检测azure blob存储文件的更改并删除该文件的本地缓存实例? - How do I detect an azure blob storage file change and delete local cache instance of that file? 如何从Azure Blob存储下载,解压缩和反序列化对象/文件? - How to Download, Decompress and Deserialize an object/file from Azure Blob Storage? 在asp.net中使用javascript在Azure BLOB存储上载期间,如何压缩视频? - How can i compress video during upload on Azure BLOB storage using javascript in asp.net? 如何重命名 Azure 存储(不是 Blob 存储)中的文件/目录(不是 Blob 文件)? - How to rename file/directory (not Blob file) in Azure Storage (not Blob storage)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM