C# 如何使用C中的Azure.storage.blob以ByteArray格式从Azure存储blob获取文件#
我需要使用新包Azure.storage.Blobs以字节数组格式从Azure存储获取文件。我找不到用C#做这件事的方法 您知道如何实现这一点以获取存储在Azure blob存储上的文件字节数组吗C# 如何使用C中的Azure.storage.blob以ByteArray格式从Azure存储blob获取文件#,c#,.net,azure,azure-storage,azure-storage-blobs,C#,.net,Azure,Azure Storage,Azure Storage Blobs,我需要使用新包Azure.storage.Blobs以字节数组格式从Azure存储获取文件。我找不到用C#做这件事的方法 您知道如何实现这一点以获取存储在Azure blob存储上的文件字节数组吗 感谢您的帮助。请尝试以下操作,将Blob作为流读取,然后在返回时将该流转换为字节数组: public byte[]GetFileFromAzure() { BlobServiceClient BlobServiceClient=新BlobServiceClient(“TestClient”); Blo
感谢您的帮助。请尝试以下操作,将Blob作为流读取,然后在返回时将该流转换为字节数组:
public byte[]GetFileFromAzure()
{
BlobServiceClient BlobServiceClient=新BlobServiceClient(“TestClient”);
BlobContainerClient containerClient=blobServiceClient.GetBlobContainerClient(“TestContainer”);
BlobClient BlobClient=containerClient.GetBlobClient(“te.xlsx”);
if(blobClient.ExistsAsync().Result)
{
使用(var ms=new MemoryStream())
{
blobClient.DownloadTo(ms);
返回ToArray女士();
}
}
返回新字节[];//返回空数组
}
blobClient有一个名为DownloadToByteArray()
@DavidTansey的方法:没有,只有4个方法,Donwload、downloadsync、DownloadTo、DownloadToAsyncI。谢谢你。请参阅我的答案,了解使用BlobClient.DownloadTo(stream)
@DavidTansey:是的,下面的代码按预期工作。
public byte[] GetFileFromAzure()
{
byte[] filebytes;
BlobServiceClient blobServiceClient = new BlobServiceClient( "TestClient");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("TestContainer");
BlobClient blobClient = containerClient.GetBlobClient("te.xlsx");
if (blobClient.ExistsAsync().Result)
{
var response = blobClient.DownloadAsync().Result;
using (var streamReader = new StreamReader(response.Value.Content))
{
var line = streamReader.ReadToEnd();
//No idea how to convert this to ByteArray
}
}
return filebytes;
}