简体   繁体   English

从Azure Blob存储下载图像

[英]Downloading images from Azure Blob Storage

I'm writing a program that can download images from specified file paths from our Azure Storage container. 我正在编写一个可以从Azure存储容器中的指定文件路径下载图像的程序。

I have put together the following function from code found online and then removing the errors. 我已经从在线找到的代码中汇总了以下函数,然后删除了错误。 However, the final errors I can't get rid of. 但是,最后的错误我无法摆脱。

Full code below: 完整代码如下:

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());

        }
    }
   }

The code currently errors on RoleEnvironment saying the namespace doesn't exist, am I missing an assembly reference? RoleEnvironment上的代码当前错误说命名空间不存在,我错过了程序集引用吗? Or do I need to import another using . 或者我需要using导入另一个。 The second error is on all of the Result. 第二个错误出现在所有Result. again stating Response doesn't exist. 再次声明Response不存在。

Is this also the best approach to take regarding downloading images from BlobStorage or should I take another approach at it? 这也是从BlobStorage下载图像的最佳方法,还是我应采取另一种方法?

EDIT: I resolved the RoleEnvironment error with the help of the given answer. 编辑:我在给定答案的帮助下解决了RoleEnvironment错误。 Now trying to determine what the Response section of the code needs changing too. 现在尝试确定代码的响应部分也需要更改。

        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");
        }
    }

The RoleEnvironment class is part of the Microsoft.WindowsAzure.ServiceRuntime namespace which I know from the old classic Cloud Services. RoleEnvironment类是Microsoft.WindowsAzure.ServiceRuntime命名空间的一部分,我从旧的经典云服务中知道。

You should probably use the ConfigurationManager to retrieve your settings. 您应该使用ConfigurationManager来检索您的设置。

See Configuration in ASP.NET Core or if you use the .NET full framework read more about it here . 请参阅ASP.NET Core中的配置,或者如果您使用.NET完整框架,请在此处阅读更多相关信息。

If you want to download images to local disk, your code could be modified as below: 如果要将映像下载到本地磁盘,可以按如下方式修改代码:

    /// <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);
        }
    }

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

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