Dafna
Dafna

Reputation: 191

How to upload a folder to Azure storage

I would like to upload an entire folder to azure storage. I know I can upload a file using:

blobReference.UploadFromFile(fileName);

But could not find a method to upload an entire folder (recursively). Is there such method? Or maybe an example code?

Thanks

Upvotes: 4

Views: 14450

Answers (4)

Dima
Dima

Reputation: 2909

From the CLI you can use:

az storage blob upload-batch

https://learn.microsoft.com/en-us/cli/azure/storage/blob?view=azure-cli-latest#az-storage-blob-upload-batch

Upvotes: 1

Zhaoxing Lu
Zhaoxing Lu

Reputation: 6467

You can try Microsoft Azure Storage DataMovement Library which support transferring blob directory with high-performance, scalability and reliability. In addition, it supports cancelling and then resuming during transferring. Here is a sample of uploading a folder to Azure Blob Storage.

Upvotes: 1

Niels
Niels

Reputation: 1075

The folder structure can simply be part of the filename:

string myfolder = "datadir";
string myfilename = "mydatafile";
string fileName = String.Format("{0}/{1}.csv", myfolder, myfilename);
CloudBlockBlob blob = container.GetBlockBlobReference(fileName);

If you upload like this example, files would appear in the container in the 'datadir' folder.

That means that you can use this to copy a directory structure to upload:

foreach (string file in Directory.EnumerateFiles(path, "*.*", SearchOption.AllDirectories)) {
    // file would look like "C:\dir1\dir2\blah.txt"

    // Don't know if this is the prettiest way, but it will work:
    string cloudfilename = file.Substring(3).Replace('\\', '/');

    // get the blob reference and push the file contents to it:
    CloudBlockBlob blob = container.GetBlockBlobReference(cloudfileName);
    blob.UploadFromFile(file);
  }

Upvotes: 13

Zed
Zed

Reputation: 178

The command line does not have an option to bulk upload multiple files in one invocation. However, you can either use find or a loop to upload multiple files like this for example:

#!/bin/bash

export AZURE_STORAGE_ACCOUNT='your_account'
export AZURE_STORAGE_ACCESS_KEY='your_access_key'

export container_name='name_of_the_container_to_create'
export source_folder=~/path_to_local_file_to_upload/*


echo "Creating the container..."
azure storage container create $container_name

for f in $source_folder
do
  echo "Uploading $f file..."
  azure storage blob upload $f $container_name $(basename $f)
  cat $f
done

echo "Listing the blobs..."
azure storage blob list $container_name

echo "Done"

Upvotes: 1

Related Questions