【问题标题】:How to read/Download all files from a Azure container/Blob using C#?如何使用 C# 从 Azure 容器/Blob 读取/下载所有文件?
【发布时间】:2015-05-07 16:49:03
【问题描述】:

我是 Azure 存储的新手。因此,请考虑帖子中的文本/代码。 我有一些文件存储在Azure Storage

示例:

http://my.blob.core.windows.net/2015/4/10/fea1fc9d-d04102015115229.jpg
http://my.blob.core.windows.net/2015/4/10/asdfc9d-d04102015115229.jpg

现在我想使用Container/Blob 名称将这些文件下载到特定文件夹。

下载文件.aspx

protected void Callme(string sourceURLPath)
    {
        string thumbDirectoryName = string.Empty;
        string sourcePath = sourceURLPath;
        string targetUrl = string.Empty;

        CloudStorageAccount cloudStorageAccount;
        CloudBlobClient blobClient;
        CloudBlobContainer blobContainer;
        BlobContainerPermissions containerPermissions;
        CloudBlob blob;
        cloudStorageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=" + ConfigurationManager.AppSettings["AzureStorageAccountName"] + ";AccountKey=" + ConfigurationManager.AppSettings["AzureStorageAccountKey"] + "");
 blobClient = cloudStorageAccount.CreateCloudBlobClient();
  blobContainer = blobClient.GetContainerReference(DateTime.Now.Year.ToString());
  blobContainer.CreateIfNotExist();
 containerPermissions = new BlobContainerPermissions();
  containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
        blobContainer.SetPermissions(containerPermissions);
//need to get files here and download in to specific folder
}

有什么建议吗?

【问题讨论】:

    标签: c# file azure download azure-cloud-services


    【解决方案1】:

    你快到了:)。以下是您需要做的:

    • 首先,您必须列出容器中的 blob。为此,您可以在容器上使用 ListBlobs 方法。列出 blob 时,请确保将 prefix 传递为 empty string (string.Empty) 并将 useFlatBlobListing 传递为 true。这将提供容器中的 blob 列表。
    • 接下来,您将遍历 blob 列表,将每个 blob 转换为 CloudBlockBlob,然后对每个 blob 调用 DownloadToFile 方法。

    关于您的代码的一些建议:

    • 由于您正在从 blob 容器读取文件,因此该容器已经存在。因此无需尝试在每个请求上创建该容器。或者换句话说,从你的代码中去掉这一行:

      blobContainer.CreateIfNotExist();

    • 同样,您也不需要以下代码行,因为您所做的只是读取 blob。

      containerPermissions = new BlobContainerPermissions(); containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob; blobContainer.SetPermissions(containerPermissions);

    【讨论】:

    • 嗨@Gaurav Mantri,实际上天蓝色上有很多容器,所以我想获取天蓝色上的所有容器以及要下载到本地文件夹的目录和子目录中的所有文件?请为上述要求添加示例代码吗?
    • 请参阅有关如何从存储帐户中获取所有容器的信息:stackoverflow.com/questions/30076886/…
    猜你喜欢
    • 2020-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2020-10-13
    • 2014-09-15
    • 2020-07-15
    • 1970-01-01
    相关资源
    最近更新 更多