azure - 如何使用java在azure blob容器之间移动文件?

标签 azure azure-storage azure-blob-storage azure-java-sdk

如何使用 java api 将文件从一个 Blob 容器移动到另一个容器。

我正在使用来自微软的以下 SKD。

Gradle dependency: compile group: 'com.azure', name: 'azure-storage-blob', version: '12.8.0'
 

如何使用 java api 在 blob 存储之间移动文件。

最佳答案

BlobClientBase.beginCopy方法用于将源 URL 处的数据复制到 blob。

代码示例:

如果容器位于不同的存储帐户中:

String connectStr = "source storage account connection string";
String destconnectStr="destination storage account connection string";

// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

BlobServiceClient destblobServiceClient = new BlobServiceClientBuilder().connectionString(destconnectStr).buildClient();

BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient("test");

BlobContainerClient destcontainer=destblobServiceClient.getBlobContainerClient("destcontainer");

PagedIterable<BlobItem> blobs= containerClient.listBlobs();
for (BlobItem blobItem : blobs) {

    System.out.println("This is the blob name: " + blobItem.getName());
    BlobClient blobClient=containerClient.getBlobClient(blobItem.getName());
    BlobClient destblobclient=destcontainer.getBlobClient(blobItem.getName());
    destblobclient.beginCopy(blobClient.getBlobUrl(),null);

}

如果容器位于同一存储帐户中:

String connectStr = "storage account connection string";

// Create a BlobServiceClient object which will be used to create a container client
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();

BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient("test");

BlobContainerClient destcontainer=blobServiceClient.getBlobContainerClient("testcontainer");

PagedIterable<BlobItem> blobs= containerClient.listBlobs();
for (BlobItem blobItem : blobs) {

    System.out.println("This is the blob name: " + blobItem.getName());
    BlobClient blobClient=containerClient.getBlobClient(blobItem.getName());
    BlobServiceSasSignatureValues sas = new BlobServiceSasSignatureValues(OffsetDateTime.now().plusHours(1),
    BlobContainerSasPermission.parse("r"));
    String sasToken = blobClient.generateSas(sas);

    BlobClient destblobclient=destcontainer.getBlobClient(blobItem.getName());
    destblobclient.beginCopy(blobClient.getBlobUrl()+ "?" + sasToken,null);

}

BlobClientBase.copyFromUrl方法也可用于此目的,但它将等待复制完成后再返回响应。您可以选择您需要的内容。

关于azure - 如何使用java在azure blob容器之间移动文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64870147/

相关文章:

azure - 我是否需要 Azure Blob 存储,还是只需要 VM 上的简单 Web 服务器?

Azure 应用服务 + Docker 自动部署?

azure - 在 Azure DevOps Server 上创建构建 zip,例如 github

powershell - 更改/重命名AzureReservedIP的ReservedIPName

c# - Dot Net Core 和 Azure 存储 : Could not load file or assembly System, 版本=4.0.0.0

node.js - java.net.SocketInputStream.read(来源未知)处的连接重置 - Node.js - Azure 文件服务

linux - 如何使用 blobxfer 从 azure blob 存储下载子目录

azure - 如何在 ARM 模板中包含外部逻辑应用实现

c# - 用于常见 REST API 错误代码的 Azure 存储 API

azure - 具有 blob 触发器的可扩展 Azure 函数