0
我有一個XDocument作爲參數提供,需要壓縮並上傳到Azure文件存儲。這裏有一些問題部分地回答了這個問題,但是對於blob存儲。簽名是不同的,我不能讓它適用於我的情況,沒有在雲中獲取壓縮文件的校驗和錯誤。在內存中壓縮XML文件並將其上傳到azure文件存儲
public static bool ZipAndUploadDocumentToAzure(XDocument doc, Configuration config)
{
if (IsNullOrEmpty(config.StorageAccountName) ||
IsNullOrEmpty(config.StorageAccountKey) ||
IsNullOrEmpty(config.StorageAccounyShareReference) ||
IsNullOrEmpty(config.StorageAccounyDirectoryReference))
{
return false;
}
CloudStorageAccount storageAccount = CloudStorageAccount.Parse($"DefaultEndpointsProtocol=https;AccountName={config.StorageAccountName};AccountKey={config.StorageAccountKey}");
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference(config.StorageAccounyShareReference);
CloudFileDirectory root = share.GetRootDirectoryReference();
CloudFileDirectory dir = root.GetDirectoryReference(config.StorageAccounyDirectoryReference);
var cloudFile = dir.GetFileReference("myarchive.zip");
using (var stream = new MemoryStream())
{
var xws = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true
};
using (XmlWriter xw = XmlWriter.Create(stream, xws))
{
doc.WriteTo(xw);
}
//where to actually use ZipArchive 'using block' without saving
//any of the zip or XML or XDocument files locally
//and that it doesn't produce checksum error
cloudFile.UploadFromStream(stream);//this gives me XML file
//saved to file storage
return true;
}
}
謝謝!那做了這個工作。 –