2

I'm trying to download all Blobs from a single Azure container using code. When I download each Blob I want the new file names to be consecutive numbers from 2.jpg upwards through 3.jpg, 4.jpg 5.jpg etc. This is not the name of the Blobs in the azure container, I dont know the name of each Blob when the code runs.

I seem to be falling at the last hurdle, I cant figure out what to put into my foreach block to get the files into the \home\pi\Pictures\ directory on my local hard drive.

urls.DownloadToStream(FileStream); is throwing an error that

List String does not contain a definition for DownloadToStream

and

FileStream is a type which is not valid in the given context.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Auth;
using Microsoft.WindowsAzure.Storage.Blob;
using System.Configuration;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Threading;

namespace CPGetAdverts
{
    class Program
    {
        static void Main(string[] args)
        {
            // Retrieve storage account from connection string.
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["AzureImagesConnection"].ConnectionString);
            // Create the blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            // Retrieve a reference to a container.
            var container = blobClient.GetContainerReference("xxxxxx/").ListBlobs();
            // Retrieve filenames from container List
            var urls = new List<string>();
            int fileName = 2;
            foreach (var blob in container)
                {
                using (var fileStream = System.IO.File.OpenWrite(@"\home\pi\Pictures\"+ fileName + ".jpg")) ;
                urls.DownloadToStream(FileStream);
                fileName++;
                }

        }
    }
}

Any help is very much appreciated.

DMur
  • 625
  • 13
  • 26

1 Answers1

2

A couple issues with this line:

urls.DownloadToStream(FileStream);
  • urls is a list of strings so it won't contain the DownloadToStream method
  • FileStream should be fileStream

Try changing your loop to something like:

foreach (var blob in container)
{
    using (var fileStream = System.IO.File.OpenWrite(@"\home\pi\Pictures\"+ fileName + ".jpg"))
    {
        var blobReference = blobClient.GetBlobReferenceFromServer(blob.Uri);
        blobReference.DownloadToStream(fileStream);
        fileName++;
    }
}
  • Thanks for the answer, I've run into another problem (http://stackoverflow.com/questions/35188670/azure-connection-string-object-reference-not-set-to-an-instance-of-an-object) so cant test this yet but it looks solid. I appreciate the advice. – DMur Feb 03 '16 at 22:05