gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:34:14 24 4
gpt4 key购买 nike

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

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

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

copyBlobClient.copyFromUrl(sourceBlobClient.getBlobUrl());

但是,当在集群中执行此操作时,我收到以下错误

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

消息显示“资源不存在”,但该 blob 显然存在。不过,我的容器具有私有(private)访问权限。

现在,当我将公共(public)访问级别更改为“Blob(仅对 blob 进行匿名读取访问)”时,一切都会正常工作。但是,我不接受公共(public)访问。

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

在我的情况下我可能会错过什么或配置错误?

最后是错误消息本身。有一个部分写着“CannotVerifyCopySource”,这可以帮助您了解某些内容具有访问权限,但消息部分显然具有误导性。难道不应该更明确地说明错误吗?

最佳答案

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

  • 在存储帐户之间复制 blob

如果使用 Azure MSI 在存储帐户之间复制 blob。我们应该采取以下行动

  1. 将 Azure 存储 Blob 数据读取器分配给源容器中的 MSI

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

  3. 为 blob 生成 SAS token 。如果源 blob 是公共(public)的,我们可以直接使用源 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();
}

enter image description here

  • 复制到同一帐户

如果使用 Azure MSI 在同一存储帐户中复制 Blob,我建议您将存储 Blob 数据贡献者分配给存储帐户中的 MSI。然后我们可以使用copyFromUrl方法进行复制操作。

例如

a.在帐户级别将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();
}

enter image description here

更多详情请引用herehere

关于azure - 使用 Java v12 SDK 在 Azure Blob 存储中复制 Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63946587/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com