使用服务帐户从 Azure Blob 存储中批量删除文件
Delete files in batch from azure blob storage using service account
我正在使用 azure blob 存储来存储我的项目文件。
我有一个 Azure Blob 存储服务帐户(client_id 和 client_secret)。我使用 StorageCredentialsToken
创建了 CloudBlobClient
,如下所示:
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");
现在使用 CloudBlobContainer
我一次可以删除一个文件:
CloudBlockBlob blockBlobReference = cloudBlobContainer.getBlockBlobReference(key);
if (blockBlobReference.exists()) {
blockBlobReference.delete();
}
如何使用一次调用删除多个文件?
我发现 this 文档说我们可以使用 BlobBatchClient
删除多个文件。在文档中,我找不到任何使用服务帐户创建 BlobBatchClient
的方法(使用 client_id 和 client_secret 获得的访问令牌)。
我需要删除100个文件,我们可以在异步调用中删除文件吗?
有没有批量删除文件的替代解决方案?
SDK版本compile group: 'com.microsoft.azure', name: 'azure-storage', version: '8.6.5'
根据 Jim 的评论,我使用访问令牌创建了 BlobServiceAsyncClient
示例方法:
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 依赖关系:
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'
我正在使用 azure blob 存储来存储我的项目文件。
我有一个 Azure Blob 存储服务帐户(client_id 和 client_secret)。我使用 StorageCredentialsToken
创建了 CloudBlobClient
,如下所示:
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");
现在使用 CloudBlobContainer
我一次可以删除一个文件:
CloudBlockBlob blockBlobReference = cloudBlobContainer.getBlockBlobReference(key);
if (blockBlobReference.exists()) {
blockBlobReference.delete();
}
如何使用一次调用删除多个文件?
我发现 this 文档说我们可以使用 BlobBatchClient
删除多个文件。在文档中,我找不到任何使用服务帐户创建 BlobBatchClient
的方法(使用 client_id 和 client_secret 获得的访问令牌)。
我需要删除100个文件,我们可以在异步调用中删除文件吗? 有没有批量删除文件的替代解决方案?
SDK版本compile group: 'com.microsoft.azure', name: 'azure-storage', version: '8.6.5'
根据 Jim 的评论,我使用访问令牌创建了 BlobServiceAsyncClient
示例方法:
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 依赖关系:
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'