[英]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.