如何将文件夹上传到 Azure 存储

How to upload a folder to Azure storage

我想将整个文件夹上传到 Azure 存储。 我知道我可以使用以下方式上传文件:

blobReference.UploadFromFile(fileName);

但找不到上传整个文件夹的方法(递归)。 有这样的方法吗?或者示例代码?

谢谢

命令行没有在一次调用中批量上传多个文件的选项。但是,您可以使用查找或循环来上传多个文件,例如:

#!/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"

文件夹结构可以是文件名的一部分:

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

如果您像本示例一样上传,文件将出现在 'datadir' 文件夹的容器中。

也就是说你可以用这个复制一个目录结构来上传:

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

您可以试试 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 是将文件夹上传到 Azure Blob 存储的示例。

您可以从 CLI 使用:

az storage blob upload-batch

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