我正在编写一个程序,可以从Azure存储容器中的指定文件路径下载图像。
我已将在线代码中的以下函数放在一起,然后删除错误。但是,我无法摆脱最后的错误。
以下完整代码:
using System;
using System.Windows.Forms;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using System.IO;
namespace WarehousePhotoProgram
{
public partial class Form1 : Form
{
string storageConnectionString = Environment.GetEnvironmentVariable("storageconnectionstring");
public Form1()
{
InitializeComponent();
}
private void DownloadFileFromBlob(string fileName, string containerName)
{
CloudStorageAccount account = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue(storageConnectionString));
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlob blob = container.GetBlobReference(fileName);
MemoryStream memStream = new MemoryStream();
blob.DownloadToStream(memStream);
Response.ContentType = blob.Properties.ContentType;
Response.AddHeader("Content-Disposition", "Attachment; filename=" + fileName.ToString());
Response.AddHeader("Content-Length", blob.Properties.Length.ToString());
Response.BinaryWrite(memStream.ToArray());
}
}
}
RoleEnvironment
上的代码当前错误,说明命名空间不存在,我是否缺少程序集引用?或者我是否需要导入另一个using
。第二个错误出现在Result.
的所有Response
上,表明RoleEnvironment
不存在。
这也是从BlobStorage下载图像的最佳方法,还是应该采用其他方法?
编辑:我在给定答案的帮助下解决了 public Form1()
{
InitializeComponent();
//_storageAccount = CloudStorageAccount.Parse(storageConnectionString);
DownloadFileFromBlob("029000/1.png", "warehouseimages", "Desktop");
}
private void DownloadFileFromBlob(string fileName, string containerName, string localFilePath)
{
CloudStorageAccount account = CloudStorageAccount.Parse(storageConnectionString);
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlob blob = container.GetBlobReference(fileName);
using (var fileStream = System.IO.File.OpenWrite(localFilePath))
{
blob.DownloadToStream(fileStream);
MessageBox.Show("SUCCESS");
}
}
错误。现在尝试确定代码的Response部分也需要改变。
SurfaceView
答案 0 :(得分:2)
RoleEnvironment类是Microsoft.WindowsAzure.ServiceRuntime命名空间的一部分,我从旧的经典云服务中知道。
您应该使用 ConfigurationManager 来检索您的设置。
请参阅Configuration in ASP.NET Core或者如果您使用.NET完整框架,请阅读更多相关信息here。
答案 1 :(得分:1)
如果要将图像下载到本地磁盘,可以按如下方式修改代码:
/// <summary>
/// Download File From Blob
/// </summary>
/// <param name="fileName">For example: image.PNG</param>
/// <param name="containerName">container name of blob</param>
/// <param name="localFilePath">For example: @"C:\Test\BlobTest.PNG"</param>
private void DownloadFileFromBlob(string fileName, string containerName, string localFilePath)
{
CloudStorageAccount account = CloudStorageAccount.Parse("Your connection string");
CloudBlobClient blobClient = account.CreateCloudBlobClient();
CloudBlobContainer container = blobClient.GetContainerReference(containerName);
CloudBlob blob = container.GetBlobReference(fileName);
using (var fileStream = System.IO.File.OpenWrite(localFilePath))
{
blob.DownloadToStream(fileStream);
}
}