如何使用Java/REST将Azure Blob从一个存储容器移动到另一个存储容器? [英] How to move Azure blob from one Storage container to another using Java/ REST?

查看:121
本文介绍了如何使用Java/REST将Azure Blob从一个存储容器移动到另一个存储容器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮忙吗?我正在关注Java JDK示例,其中有许多有关如何管理容器和Blob的示例,但是,没有任何内容说明如何从一个存储容器移动到另一个存储容器.

例如,我在StorageOne/ContainerOne/BlobName上有一个Blob,需要将其移至Storage2/ContainerTwo/BlobName

我正在查看此网站

更新:

 字符串connectStr =源存储帐户连接字符串";String destconnectStr =目标存储帐户连接字符串";//创建一个BlobServiceClient对象,该对象将用于创建容器客户端BlobServiceClient blobServiceClient =新的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();为(BlobItem blobItem:blobs){System.out.println(这是Blob名称:" + blobItem.getName());BlobClient blobClient = containerClient.getBlobClient(blobItem.getName());BlobClient destblobclient = destcontainer.getBlobClient(blobItem.getName());destblobclient.beginCopy(blobClient.getBlobUrl(),null);} 

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

解决方案

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 = "storage account connection string";

        // Create a BlobServiceClient object which will be used to create a container client
        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);

        }

Update:

        String connectStr = "source storage account connection string";

        String destconnectStr="destination storage account connection string";



        // 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("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);

        }

这篇关于如何使用Java/REST将Azure Blob从一个存储容器移动到另一个存储容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆