如何使用Java SDK将Azure存储blob移动到其他容器?

toe95027  于 2023-02-05  发布在  Java
关注(0)|答案(1)|浏览(153)

我正在尝试将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内部服务器错误。
接下来我可以尝试什么?

swvgeqrz

swvgeqrz1#

我认为出现这个错误的原因是您在创建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

相关问题