简体   繁体   中英

Downloading images from Azure Blob Storage

I'm writing a program that can download images from specified file paths from our Azure Storage container.

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? Or do I need to import another using . The second error is on all of the Result. again stating Response doesn't exist.

Is this also the best approach to take regarding downloading images from BlobStorage or should I take another approach at it?

EDIT: I resolved the RoleEnvironment error with the help of the given answer. 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.

You should probably use the ConfigurationManager to retrieve your settings.

See Configuration in ASP.NET Core or if you use the .NET full framework read more about it here .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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