![](/img/trans.png)
[英]How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C#
[英]how to retrieve an xml file from azure blob storage using c#
我想從 Blob 存儲中檢索 2 個 Xml 文件並使用 c# 返回這些文件。 無需擔心連接、容器名稱和文件名。 我嘗試使用下面的代碼從 blob 中獲取 xml 作為字符串並作為列表返回,但我需要一個作為 Xmlfile 返回。
public List<string> GetXmlFiles(List<string> Xmlname)
{
string storageConnectionString = "";
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("ContainerName");
CloudBlockBlob blob = container.GetBlockBlobReference("fileName");
string xml =blob.DownloadTextAsync().ToString();
List<string> XmlFile = new List<string>(xml.Split(' '));
return XmlFile;
}
嘗試使用此代碼,我在我的系統中進行了測試,能夠將 xml 文件下載為文件
using Microsoft.Azure.Storage;
using Microsoft.Azure.Storage.Blob;
using System;
using System.IO;
namespace downloadxml
{
class Program
{
static void Main(string[] args)
{
string storageConnectionString = "Connection string”
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference("test");
var allblobs = container.ListBlobs(useFlatBlobListing: true);
List<String> files = new List<String>();
foreach (var blob in allblobs)
{
string name = ((CloudBlockBlob)blob).Name;
CloudBlockBlob blockBlob = container.GetBlockBlobReference(name);
string path = (@"localk path”)
string[] names = name.Split("/");
string newPath = "";
string fileName = "";
for (int i = 0; i < names.Length; i++)
{
if (i != (names.Length - 1))
{
path = path + names[i] + "\\";
newPath = newPath + "\\" + names[i];
}
fileName = names[(names.Length - 1)];
}
string filePath = path + fileName;
if (Directory.Exists(path))
{
blockBlob.DownloadToFile(filePath, FileMode.OpenOrCreate);
}
files.Add(filePath);
}
}
}
}
輸出
我有兩個 xml 文件xmlfile1
和xmlfile2
在本地將兩個文件下載為xmlfile1
和xmlfile2
作為 xml 文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.