英文:
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<String> files) {
String endpoint = "https://azureaccount.blob.core.windows.net/";
AccessToken accessToken = new AccessToken("access token created with client id and client secret", 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("Can not encode blob name={}", name);
}
});
blobBatchClient.deleteBlobs(blobUrls, DeleteSnapshotsOptionType.INCLUDE).forEach(response -> {
LOGGER.debug("File with name={} deleted, status code={}", response.getRequest().getUrl(), response.getStatusCode());
}
);
}
Gradle dependencies:
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论