从Azure Blob存储中批量使用服务帐户删除文件。

huangapple go评论80阅读模式
英文:

Delete files in batch from azure blob storage using service account

问题

以下是翻译好的内容:

我正在使用 Azure Blob 存储来存储我的项目文件。

我有一个 Azure Blob 存储的服务帐户(client_id 和 client_secret)。我已经使用以下方式创建了 CloudBlobClient,并使用 StorageCredentialsToken

StorageCredentialsToken credentialsToken = new StorageCredentialsToken("帐户名称", "使用 client_id 和 client_secret 生成的访问令牌");
CloudBlobClient blobClient = new CloudBlobClient(new URI("https://accountname.blob.core.windows.net/"), credentialsToken);
CloudBlobContainer cloudBlobContainer = blobClient.getContainerReference("容器名称");

现在,使用 CloudBlobContainer,我可以逐个删除文件:

CloudBlockBlob blockBlobReference = cloudBlobContainer.getBlockBlobReference(key);
if (blockBlobReference.exists()) {
    blockBlobReference.delete();
}

如何通过单个调用删除多个文件?

我找到了这个文档,它说我们可以使用 BlobBatchClient 删除多个文件。在文档中,我找不到任何使用服务帐户(使用 client_id 和 client_secret 获得的访问令牌)创建 BlobBatchClient 的方法。

在异步调用中是否可以删除文件?因为我需要删除数百个文件。
有没有批量删除文件的替代方法?

SDK 版本为 compile group: 'com.microsoft.azure', name: 'azure-storage', version: '8.6.5'

英文:

I am using azure blob storage to store my project files.

I have a service account of azure blob storage(client_id and client_secret).I have created CloudBlobClient using StorageCredentialsToken as below:

StorageCredentialsToken credentialsToken = new StorageCredentialsToken("account name", "access token generated uing client_id and client_secret");
CloudBlobClient blobClient = new CloudBlobClient(new URI("https://accountname.blob.core.windows.net/"), credentialsToken);
CloudBlobContainer cloudBlobContainer = blobClient.getContainerReference("conteiner name");

Now using CloudBlobContainer I can delete one file at a time:

CloudBlockBlob blockBlobReference = cloudBlobContainer.getBlockBlobReference(key);
if (blockBlobReference.exists()) {
    blockBlobReference.delete();
}

How can I delete multiple files using a single call?

I find this doc which says we can delete multiple files with BlobBatchClient. In the document, I can not find any ways to create BlobBatchClient using a service account(using access token obtained by client_id and client_secret).

Can we delete files in async call as I need to delete 100s of files?
Any alternative solutions to delete files in batch?

SDK version compile group: 'com.microsoft.azure', name: 'azure-storage', version: '8.6.5'

答案1

得分: 0

根据 Jim 的评论,我已使用访问令牌创建了 BlobServiceAsyncClient 示例方法:

public void delete(List<String> files) {
    String endpoint = "https://azureaccount.blob.core.windows.net/";
    AccessToken accessToken = new AccessToken("使用客户端 ID 和客户端密钥创建的访问令牌", OffsetDateTime.now().plusHours(1)); 
    BlobServiceAsyncClient storageClient = new BlobServiceClientBuilder().credential(request -> Mono.just(accessToken))
            .endpoint(endpoint)
            .buildAsyncClient();
    BlobBatchClient blobBatchClient = new BlobBatchClientBuilder(storageClient).buildClient();
    List<String> blobUrls = new ArrayList<>();
    files.forEach(name -> {
        try {
            String blobUrl = endpoint + "conteinerName/" + URLEncoder.encode(name, "UTF-8");
            blobUrls.add(blobUrl);
        } catch (UnsupportedEncodingException e) {
            LOGGER.debug("无法编码 Blob 名称={}", name);
        }
    });
    blobBatchClient.deleteBlobs(blobUrls, DeleteSnapshotsOptionType.INCLUDE).forEach(response -> {
                LOGGER.debug("已删除文件,名称={},状态码={}", response.getRequest().getUrl(), response.getStatusCode());
            }
    );
}

Gradle 依赖项:

compile group: 'com.azure', name: 'azure-storage-blob', version: '12.0.0'
compile group: 'com.azure', name: 'azure-storage-blob-batch', version: '12.6.0'
英文:

As per comment by Jim, I have created BlobServiceAsyncClient using access token
sample method:

public void delete(List&lt;String&gt; files) {
        String endpoint = &quot;https://azureaccount.blob.core.windows.net/&quot;;
        AccessToken accessToken = new AccessToken(&quot;access token created with client id and client secret&quot;, OffsetDateTime.now().plusHours(1)); 
        BlobServiceAsyncClient storageClient = new BlobServiceClientBuilder().credential(request -&gt; Mono.just(accessToken))
                .endpoint(endpoint)
                .buildAsyncClient();
        BlobBatchClient blobBatchClient = new BlobBatchClientBuilder(storageClient).buildClient();
        List&lt;String&gt; blobUrls = new ArrayList&lt;&gt;();
        files.forEach(name -&gt; {
            try {
                String blobUrl = endpoint + &quot;conteinerName/&quot; + URLEncoder.encode(name, &quot;UTF-8&quot;);
                blobUrls.add(blobUrl);
            } catch (UnsupportedEncodingException e) {
                LOGGER.debug(&quot;Can not encode blob name={}&quot;, name);
            }
        });
        blobBatchClient.deleteBlobs(blobUrls, DeleteSnapshotsOptionType.INCLUDE).forEach(response -&gt; {
                    LOGGER.debug(&quot;File with name={} deleted, status code={}&quot;, response.getRequest().getUrl(), response.getStatusCode());
                }
        );
}

Gradle dependencies:

compile group: &#39;com.azure&#39;, name: &#39;azure-storage-blob&#39;, version: &#39;12.0.0&#39;
compile group: &#39;com.azure&#39;, name: &#39;azure-storage-blob-batch&#39;, version: &#39;12.6.0&#39;

huangapple
  • 本文由 发表于 2020年10月19日 16:30:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/64423714.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定