简体   繁体   中英

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

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);

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM