从 Azure Blob 存储下载图像
Downloading images from Azure Blob Storage
我正在编写一个程序,可以从我们的 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
不存在。
这也是从 BlobStorage 下载图像的最佳方法吗?还是我应该采用其他方法?
编辑:我在给定答案的帮助下解决了 RoleEnvironment
错误。现在尝试确定代码的响应部分也需要更改。
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");
}
}
RoleEnvironment class 是 Microsoft.WindowsAzure.ServiceRuntime 命名空间的一部分,我从旧的 classic 云服务中了解到它。
您可能应该使用 ConfigurationManager 来检索您的设置。
见Configuration in ASP.NET Core or if you use the .NET full framework read more about it here。
如果你想下载图片到本地磁盘,你的代码可以修改如下:
/// <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);
}
}
我正在编写一个程序,可以从我们的 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
不存在。
这也是从 BlobStorage 下载图像的最佳方法吗?还是我应该采用其他方法?
编辑:我在给定答案的帮助下解决了 RoleEnvironment
错误。现在尝试确定代码的响应部分也需要更改。
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");
}
}
RoleEnvironment class 是 Microsoft.WindowsAzure.ServiceRuntime 命名空间的一部分,我从旧的 classic 云服务中了解到它。
您可能应该使用 ConfigurationManager 来检索您的设置。
见Configuration in ASP.NET Core or if you use the .NET full framework read more about it here。
如果你想下载图片到本地磁盘,你的代码可以修改如下:
/// <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);
}
}