如何使用Java/REST将Azure Blob从一个存储容器移动到另一个容器?

huangapple go评论135阅读模式
英文:

How to move Azure blob from one Storage container to another using Java/ REST?

问题

有人可以帮助吗?我正在按照Java JDK示例,有很多关于如何管理容器和Blob的示例,但是没有说明如何从一个存储容器移动到另一个容器。

例如,我有一个Blob在StorageOne/ContainerOne/BlobName上,需要移动到Storage2/ContainerTwo/BlobName上。

我正在查看这个网站https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/storage/azure-storage-blob/README.md,但没有运气。

另外,我已经成功通过ConnectionString连接并创建、下载Blob,但无法弄清如何移动。

任何建议都会有帮助。我还尝试在Azure中创建一个应用函数来执行此操作,但我的PowerShell技能不太好。

谢谢

英文:

Can someone help with this please? I am following the Java JDK samples, there are lots of examples on how to manage containers and blobs, however, nothing saying how to move from one storage container onto another.

Eg I have a blob on StorageOne/ContainerOne/BlobName need to be moved to Storage2/ContainerTwo/BlobName

I am looking at this site https://github.com/Azure/azure-sdk-for-java/blob/master/sdk/storage/azure-storage-blob/README.md hoever no luck.

Also I have managed to connect via ConnectionString and create, download blobs fine, however cant figure out how to move.

Any suggestion would be helpful. I have also tried to create an App Function in Azure to do it, but my powershell skills are not good.

Thank you

答案1

得分: 6

如果您想将一个存储容器中的 Blob 复制到另一个存储容器中,您可以使用 beginCopy 方法,首先使用 getBlobUrl 方法获取源 Blob 的 URL,然后传递它。

如果您需要示例代码,可以参考这个 GitHub 示例:BlobAsyncClientBaseJavaDocCodeSnippets

如果您想要将源容器中的一个 Blob 移动到另一个容器中,并且源容器中不存在该 Blob,目前没有直接的方法来实现。您可以先复制该 Blob,然后在复制完成后使用 delete 方法删除源 Blob。

实际上,从所有这些方法的链接中,您都可以找到它们提供的 GitHub 示例,只需按照项目结构进行操作。

更新:如果您需要示例代码,可以参考我下面的代码,我已经测试过,它可以正常工作。

String connectStr = "存储账户连接字符串";

// 创建一个 BlobServiceClient 对象,将用于创建容器客户端
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);

}

更新

String connectStr = "源存储账户连接字符串";
String destconnectStr = "目标存储账户连接字符串";

// 创建一个 BlobServiceClient 对象,将用于创建容器客户端
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);

}
英文:

If you want to copy a blob from one storage container to other storage container, you could use beginCopy method, firstly get the source blob url with getBlobUrl method then pass it.

If you want a sample you could refer to this github sample:BlobAsyncClientBaseJavaDocCodeSnippets.

And if you want to move one blob from source container to another container and it doesn't exist in the source container, for now no direct method to implement, you could copy the blob firstly, after copy activity then delete the source blob with delete method.

Actually from all these method link you could find they all provide the github sample just follow the project structure.

Update: if you want a sample code, you could refer to my below code, I have test it it could work.

        String connectStr = &quot;storage account connection string&quot;;

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

        BlobContainerClient containerClient = blobServiceClient.getBlobContainerClient(&quot;test&quot;);

        BlobContainerClient destcontainer=blobServiceClient.getBlobContainerClient(&quot;testcontainer&quot;);

        PagedIterable&lt;BlobItem&gt; blobs= containerClient.listBlobs();
        for (BlobItem blobItem : blobs) {

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

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

        }

如何使用Java/REST将Azure Blob从一个存储容器移动到另一个容器?

Update:

        String connectStr = &quot;source storage account connection string&quot;;

        String destconnectStr=&quot;destination storage account connection string&quot;;



        // 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(&quot;test&quot;);

        BlobContainerClient destcontainer=destblobServiceClient.getBlobContainerClient(&quot;destcontainer&quot;);

        PagedIterable&lt;BlobItem&gt; blobs= containerClient.listBlobs();
        for (BlobItem blobItem : blobs) {

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

        }

huangapple
  • 本文由 发表于 2020年4月9日 16:34:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/61117069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定