gpt4 book ai didi

c# - 如何使用 C# 中的 Azure.Storage.Blobs 从 Azure 存储 blob 获取 ByteArray 格式的文件

转载 作者:行者123 更新时间:2023-12-02 22:49:21 25 4
gpt4 key购买 nike

我需要使用新包 Azure.Storage.Blobs 从 Azure 存储中获取字节数组格式的文件。我无法找到在 C# 中执行此操作的方法。

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

知道如何实现获取存储在 Azure Blob 存储上的文件字节数组吗?

感谢帮助。

最佳答案

尝试以下操作将 Blob 作为流读取,然后在返回时将该流转换为字节数组:

public byte[] GetFileFromAzure()
{
BlobServiceClient blobServiceClient = new BlobServiceClient( "TestClient");
BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("TestContainer");
BlobClient blobClient = containerClient.GetBlobClient("te.xlsx");

if (blobClient.ExistsAsync().Result)
{
using (var ms = new MemoryStream())
{
blobClient.DownloadTo(ms);
return ms.ToArray();
}
}
return new byte[]; // returns empty array
}

关于c# - 如何使用 C# 中的 Azure.Storage.Blobs 从 Azure 存储 blob 获取 ByteArray 格式的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65116690/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com