为了账号安全,请及时绑定邮箱和手机立即绑定

从 Azure Blob 存储下载 blob 列表

从 Azure Blob 存储下载 blob 列表

牛魔王的故事 2023-06-04 11:09:51
我创建了CloudBlobContainer正确的连接字符串:@Bean@SneakyThrowspublic CloudBlobContainer blobContainer(CloudStorageAccount cloudStorageAccount) {    return cloudStorageAccount            .createCloudBlobClient()            .getContainerReference(containerName);}我看到使用的 blob 列表blobContainer.listBlobs()目前,我正在寻找从特定文件夹下载列表 blob 的最有效方法。
查看完整描述

3 回答

?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

listBlobs()方法有一个overload接受prefix参数。

public Iterable<ListBlobItem> listBlobs(final String prefix, final boolean useFlatBlobListing) {
        return this.listBlobs(prefix, useFlatBlobListing, EnumSet.noneOf(BlobListingDetails.class), null, null);
    }

您需要传递path of the folderasprefix和传递truefor useFlatBlobListing,这将列出该虚拟文件夹中的所有 blob。

获得该 blob 列表后,您可以使用downloadToFile每个 blob 上的方法下载 blob。



查看完整回答
反对 回复 2023-06-04
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

一段时间后,我发现我可以应用CloudBlockBlob类型ListBlobItem和下载方法。


@Bean

@SneakyThrows

public CommandLineRunner commandLineRunner(CloudBlobContainer blobContainer) {

    return args -> {

        Sets.newConcurrentHashSet(blobContainer.listBlobs("documents/"))

                .stream()

                .filter(it -> it.getUri().toString().contains("pdf"))

                .forEach(it -> {

                    ((CloudBlockBlob) it).downloadToFile(((CloudBlockBlob) it).getName());

                });


    };

}


查看完整回答
反对 回复 2023-06-04
?
互换的青春

TA贡献1797条经验 获得超6个赞

你可以通过两种方式做到这一点


(i) AZcopy-AzCopy /Source:https://myaccount.file.core.windows.net/demo/ /Dest:C:\myfolder /SourceKey:key /S


(ii) 通过Azure Cli——


# Create a directory to store all the blobs

mkdir /downloaded-container && cd /downloaded-container


# Get all the blobs

BLOBS=$(az storage blob list -c $CONTAINER \

    --account-name $ACCOUNT_NAME --sas-token "$SAS_TOKEN" \

    --query [*].name --output tsv)


# Download each one

for BLOB in $BLOBS

do

  echo "********Downloading $BLOB"

  az storage blob download -n $BLOB -f $BLOB -c $CONTAINER --account-name $ACCOUNT_NAME --sas-token "$SAS_TOKEN"

done

如果您只想通过代码,这里有一个,sample repo因为没有直接的方法可以通过 SDK 完成。

HttpGet httpGet = new HttpGet(urlString);

            signRequest(httpGet, resourcePath, account, hashFunction);

            try (CloseableHttpResponse response = httpClient.execute(httpGet)) {

                System.out.println(response.getStatusLine());


查看完整回答
反对 回复 2023-06-04
  • 3 回答
  • 0 关注
  • 139 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信