【问题标题】:How to move Azure Storage blob to other container using Java SDK?如何使用 Java SDK 将 Azure 存储 blob 移动到其他容器?
【发布时间】:2023-01-31 04:29:28
【问题描述】:

我正在尝试将 blob 从一个容器移动到另一个具有相同存储帐户的容器。我正在为此使用 Java SDK。

我的代码:

StorageSharedKeyCredential credential = new StorageSharedKeyCredential("accountname", "accountkey");
        BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint("storageaccountendpoint").credential(credential).buildClient();
        BlobContainerClient blobContainerClient =  blobServiceClient.getBlobContainerClient("failed");
        BlobClient dst = blobContainerClient.getBlobClient("https://xxxstorage.blob.core.windows.net/success/");
        BlobClient src = blobContainerClient.getBlobClient("https://xxxstorage.blob.core.windows.net/failed/Graphs.jpeg");
        dst.beginCopy(src.getBlobUrl(), null);

我必须将 blob 从失败的容器移动到成功的容器。但我面临 500 个内部服务器错误。

接下来我可以尝试什么?

【问题讨论】:

  • 请编辑您的问题并包括您到目前为止编写的代码和您遇到的问题。另外,如果您搜索这个东西,我很确定您会找到大量代码示例。
  • 更新了上述问题中的代码
  • 您的代码遇到了什么问题。
  • 更新了代码。我面临指定的 blob 不存在错误。应该给出什么作为 src blob 路径和 dest blob 路径?

标签: azure azure-storage-account azure-java-sdk


【解决方案1】:

我认为出现错误的原因是您在创建 BlobClient 时指定了 blob URL 而不是 blob 名称。此外,您没有为源容器创建 BlobContainerClient

请尝试将您的代码更改为:

StorageSharedKeyCredential credential = new StorageSharedKeyCredential("accountname", "accountkey");
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().endpoint("storageaccountendpoint").credential(credential).buildClient();
BlobContainerClient sourceBlobContainerClient =  blobServiceClient.getBlobContainerClient("success");
BlobContainerClient destinationBlobContainerClient =  blobServiceClient.getBlobContainerClient("failed");
BlobClient dst = sourceBlobContainerClient.getBlobClient("Graphs.jpeg");
BlobClient src = destinationBlobContainerClient.getBlobClient("Graphs.jpeg");
dst.beginCopy(src.getBlobUrl(), null);

请注意,我为源 blob 容器创建了一个新的 BlobContainerClient

【讨论】:

  • 嗨 Gaurav .. 我能够将 blob 从一个容器复制到另一个容器。但是源容器仍然包含 blob。理想情况下,应该在移动操作的情况下立即删除它。
  • 我需要再次从源容器中删除 blob。为此,我需要再做一个操作。是否有可能通过使用单一操作执行移动操作
  • 没有本地移动操作。复制操作完成后,您需要通过对目标 blob 执行删除操作来删除 blob。
  • 嗨 Gaurav .. 我已经发布了一个问题。你能看看这个网址吗? stackoverflow.com/questions/73868237/…
猜你喜欢
  • 1970-01-01
  • 2020-07-21
  • 2020-03-29
  • 2012-06-25
  • 2020-12-19
  • 1970-01-01
  • 2016-02-17
  • 2021-07-27
  • 2021-10-08
相关资源
最近更新 更多