使用 Java v12 SDK 在 Azure Blob 存储中复制 Blob

Copy Blob in Azure Blob Storage using Java v12 SDK

我的应用程序在 Kubernetes 集群中,我正在使用 Java v12 SDK 与 Blob 存储交互。为了对 Blob 存储进行授权,我使用了托管身份。

我的应用程序需要在一个容器内复制 blob。我还没有找到关于如何使用 SDK 进行复制的任何特定 recommendations or examples

我认为当我使用模拟器时,以下方法有效

copyBlobClient.copyFromUrl(sourceBlobClient.getBlobUrl());

但是,当它在集群中执行时,出现以下错误

<Error>
   <Code>CannotVerifyCopySource</Code>
   <Message>The specified resource does not exist. RequestId: __ Time: __ </Message>
</Error> 

消息说“资源不存在”,但 blob 显然存在。不过,我的容器有私有访问权限。

现在,当我将 public 访问级别更改为“Blob(仅限 blob 的匿名读取访问权限)”时,一切正常。但是,public 访问权限对我来说是不可接受的。

主要问题 - 使用 Java v12 SDK 实施复制 blob 的正确方法是什么。

在我的情况下我可能会遗漏或配置错误的内容?

最后是错误消息本身。有一部分写着“CannotVerifyCopySource”,这有助于您理解有一些东西可以访问,但消息部分显然具有误导性。不应该更明确地说明错误吗?

如果您想使用Azure JAVA SDK 复制blob 与Azure MSI,请参考以下详细信息

  • 在存储帐户之间复制 blob

如果您使用 Azure MSI 在存储帐户之间复制 blob。我们应该做到以下几点

  1. 将 Azure Storage Blob Data Reader 分配给源容器中的 MSI

  2. 将 Azure Storage Blob Data Contributor 分配给目标容器中的 MSI。另外当我们复制blob时,我们需要写入权限才能将内容写入blob

  3. 为 blob 生成 SAS 令牌。如果souce blob是public,我们可以直接使用source blob URL而不需要sas token。

例如

 try {
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                    .endpoint("https://<>.blob.core.windows.net/" )
                    .credential(new DefaultAzureCredentialBuilder().build())
                    .buildClient();
            // get User Delegation Key
            OffsetDateTime delegationKeyStartTime = OffsetDateTime.now();
            OffsetDateTime delegationKeyExpiryTime = OffsetDateTime.now().plusDays(7);
            UserDelegationKey key =blobServiceClient.getUserDelegationKey(delegationKeyStartTime,delegationKeyExpiryTime);

            BlobContainerClient sourceContainerClient = blobServiceClient.getBlobContainerClient("test");
            BlobClient sourceBlob = sourceContainerClient.getBlobClient("test.mp3");
            // generate sas token
            OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
            BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);

            BlobServiceSasSignatureValues myValues = new BlobServiceSasSignatureValues(expiryTime, permission)
                    .setStartTime(OffsetDateTime.now());
            String sas =sourceBlob.generateUserDelegationSas(myValues,key);

            // copy
            BlobServiceClient desServiceClient = new BlobServiceClientBuilder()
                    .endpoint("https://<>.blob.core.windows.net/" )
                    .credential(new DefaultAzureCredentialBuilder().build())
                    .buildClient();
            BlobContainerClient desContainerClient = blobServiceClient.getBlobContainerClient("test");
            String res =desContainerClient.getBlobClient("test.mp3")
                    .copyFromUrl(sourceBlob.getBlobUrl()+"?"+sas);
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }

  • 在同一个帐户中复制

如果您在与 Azure MSI 相同的存储帐户中复制 blob,我建议您将 Storage Blob Data Contributor 分配给存储帐户中的 MSI。然后我们可以用方法copyFromUrl.

做复制动作

例如

一个。将 Storage Blob Data Contributor 分配给帐户级别的 MSI

b。代码

  try {
            BlobServiceClient blobServiceClient = new BlobServiceClientBuilder()
                    .endpoint("https://<>.blob.core.windows.net/" )
                    .credential(new DefaultAzureCredentialBuilder().build())
                    .buildClient();

            BlobContainerClient sourceContainerClient = blobServiceClient.getBlobContainerClient("test");
            BlobClient sourceBlob = sourceContainerClient.getBlobClient("test.mp3");

            BlobContainerClient desContainerClient = blobServiceClient.getBlobContainerClient("output");
            String res =desContainerClient.getBlobClient("test.mp3")
                    .copyFromUrl(sourceBlob.getBlobUrl());
            System.out.println(res);
        } catch (Exception e) {
            e.printStackTrace();
        }

详情请参考here and here

我在使用 Azure 的 Java SDK 时遇到了同样的问题,我通过使用 URL + SAS 令牌复制 blob 解决了这个问题。实际上,如果您没有正确的访问权限,您通过 URL 获得的资源将不会显示为可用。这是我用来解决问题的代码:

BlobClient sourceBlobClient = blobServiceClient
                        .getBlobContainerClient(currentBucketName)
                        .getBlobClient(sourceKey);
// initializing the copy blob client
BlobClient copyBlobClient = blobServiceClient
        .getBlobContainerClient(newBucketName)
        .getBlobClient(newKey);
// Creating the SAS Token to get the permission to copy the source blob 
OffsetDateTime expiryTime = OffsetDateTime.now().plusDays(1);
BlobSasPermission permission = new BlobSasPermission().setReadPermission(true);
BlobServiceSasSignatureValues values = new BlobServiceSasSignatureValues(expiryTime, permission)
.setStartTime(OffsetDateTime.now());
String sasToken = sourceBlobClient.generateSas(values);

//Making the copy using the source blob URL + generating the copy 
var res = copyBlobClient.copyFromUrl(sourceBlobClient.getBlobUrl() +"?"+ sasToken);