繁体   English   中英

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

[英]How to get file from Azure storage blob in a ByteArray format using Azure.Storage.Blobs in C#

我需要使用新的 Azure 字节数组格式从 Azure 存储中获取文件 package Azure.Storage.Blobs。 我无法在 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 读取为 stream,然后在返回时将该 stream 转换为字节数组:

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
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM