Is it possible to copy whole blob of a container (having files)to another subscription container blob in java

巧了我就是萌 提交于 2020-04-18 05:48:42

问题


I want to copy data from blob e.g storageaccount/container/folder1/folder2/folder3 . Now I want to copy folder3 data to another subscription container blob.

I am using java and azure sdk, startcopy to copy source to destination using SAS. but everytime it says that blob does not exist.

But if source path I give like this : storageaccount/container/folder1/folder2/folder3/xyz.txt then it is able to copy data from source to destination. Cant we copy whole folder3 data to destination?instead of looping through all the files?


回答1:


You mention startcopy method, suppose you are using v8 sdk. You say when use storageaccount/container/folder1/folder2/folder3 it will say that blob does not exist, cause you just provide a directory and the startcopy need the CloudBlockBlob object.

So the right way should be list the blobs under the directory, then loop the blobs and copy the blob. The below is my test code, for test I just copy a directory to another container.

        CloudStorageAccount storageAccount = CloudStorageAccount.parse(connectStr);
        CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
        try {
            CloudBlobContainer container = blobClient.getContainerReference("test");
            Iterable<ListBlobItem> blobs=container.listBlobs("testfolder/");
            CloudBlobContainer destcontainer=blobClient.getContainerReference("testcontainer");

            for(ListBlobItem blob:blobs){
                CloudBlockBlob srcblob=new CloudBlockBlob(blob.getUri());
                CloudBlockBlob destblob= destcontainer.getBlockBlobReference(srcblob.getName());
                destblob.startCopy(srcblob);

            }

        } catch (StorageException e) {
            e.printStackTrace();
        }

Update: about the status about copy action, there is a method getCopyState, you could get the state details, hope this is what you want. More details check the method.

CopyState st=destblob.getCopyState();
System.out.println(st.getStatus());


来源:https://stackoverflow.com/questions/61176837/is-it-possible-to-copy-whole-blob-of-a-container-having-filesto-another-subscr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!