简体   繁体   English

Azure blob存储下载流返回“”asp.net

[英]Azure blob storage download to stream returning “” asp.net

I am currently trying to download a file from Azure blob storage using the DownloadToStream method to download the contents of a blob as a text string. 我目前正在尝试使用DownloadToStream方法从Azure blob存储中下载文件,以将blob的内容下载为文本字符串。 However I am not getting anything back but an empty string. 但是我没有得到任何回报,只是一个空字符串。

Here is my code that I use to connect to the azure blob container and retrieve the blob file. 这是我用来连接azure blob容器并检索blob文件的代码。

    public static string DownLoadFroalaImageAsString(string blobStorageName, string companyID)
    {
        // Retrieve storage account from connection string.
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
            CloudConfigurationManager.GetSetting("StorageConnectionString"));

        // Create the blob client.
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClient.GetContainerReference(companyID.ToLower());

        //retrieving the actual filename of the blob
        string removeString = "BLOB/";
        string trimmedString = blobStorageName.Remove(blobStorageName.IndexOf(removeString), removeString.Length);

        // Retrieve reference to a blob named "trimmedString"
        CloudBlockBlob blockBlob2 = container.GetBlockBlobReference(trimmedString);

        string text;
        using (var memoryStream = new MemoryStream())
        {
            blockBlob2.DownloadToStream(memoryStream);
            text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
        }
        return text;
    }

I was following along this documentation however I cannot seem to get it to work. 我正在关注这个文档但是我似乎无法让它工作。 Any help would be greatly appreciated. 任何帮助将不胜感激。

However I am not getting anything back but an empty string. 但是我没有得到任何回报,只是一个空字符串。

I test your supplied code on my side, it works correctly. 我测试你提供的代码在我身边,它正常工作。 I assume that the test blob content is empty in your case. 我假设您的情况下测试blob内容为空 We could trouble shooting with following ways: 我们可以通过以下方式解决问题:

1.please have a try to check the Length of memoryStream. 1.请尝试检查memoryStream的长度。 If length equal 0 we could know that the blob content is empty. 如果长度等于0,我们可以知道blob内容为空。

using (var memoryStream = new MemoryStream())
{
    blockBlob2.DownloadToStream(memoryStream);
    var length = memoryStream.Length;
    text = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
 }

2.We could upload a blob with content to container, we could do that with Azure portal or Microsoft Azure storage explorer easily. 2.我们可以将包含内容的blob上传到容器,我们可以轻松地使用Azure门户或Microsoft Azure存储资源管理器 And please have a try test it with uploaded blob. 请尝试使用上传的blob进行测试。

If you want to get the text from the blob, you can use DownloadTextAsync() 如果要从blob获取文本,可以使用DownloadTextAsync()

var text = await blockBlob2.DownloadTextAsync();

If you want to return file stream back to an API respoinse, you can use FileStreamResult which is IActionResult. 如果要将文件流返回到API respoinse,可以使用FileStreamResult,即IActionResult。

var stream = await blockBlob2.OpenReadAsync();
return File(stream, blockBlob2.Properties.ContentType, "name");

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

相关问题 Stream 视频来自 Azure blob 存储和 ASP.NET 核心 3 - Stream videos from Azure blob storage and ASP.NET Core 3 使用ASP.NET在Windows Azure blob存储上设置CORS - Set CORS on Windows Azure blob storage using ASP.NET 如何从 Asp.Net 核心中的 azure blob 下载文件? - How to download a file from azure blob in Asp.Net core? Asp.Net Core 2 + Google Cloud Storage下载内存流 - Asp.Net Core 2 + Google Cloud Storage download Memory Stream 使用ASP.Net core2在Azure存储中下载文件 - Download file in azure storage using ASP.Net core2 在Azure上移动ASP.NET Web窗体站点,而无需再次对其进行编码以使用Azure Blob存储 - Moving an asp.net web forms site on azure without coding it again to use azure blob storage 将Blob作为ASP.NET MVC中的文件下载 - Download Blob as a file in ASP.NET MVC 在asp.net中使用javascript在Azure BLOB存储上载期间,如何压缩视频? - How can i compress video during upload on Azure BLOB storage using javascript in asp.net? 如何在 ASP.NET Core 应用程序中显示来自 Azure Blob 存储帐户的图像? - How to I display an image from my Azure Blob Storage Account in my ASP.NET Core Application? 从ASP.NET C#与MS Azure上的Blob存储交互 - interacting with blob storage on MS Azure from ASP.NET C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM