我正在使用azure blob存储来存储我的项目文件。
我有一个Azure Blob存储的服务帐户(client_id和client_secret)。我CloudBlobClient
使用StorageCredentialsToken
以下方法创建了一个帐户:
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();
}
如何通过一个电话删除多个文件?
我发现这个文件说我们可以使用删除多个文件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'
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句