1

How i can move file from one blob container to another using java api.

i am using below SKD from microsoft.

Gradle dependency: compile group: 'com.azure', name: 'azure-storage-blob', version: '12.8.0'
 

how i can move a file between blob storage using java api.

Mohit Singh
  • 401
  • 1
  • 10
  • 30

1 Answers1

2

BlobClientBase.beginCopy Method is used to copy the data at the source URL to a blob.

Code sample:

If the containers are in the different storage accounts:

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

}

If the containers are in the same storage account:

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

}

BlobClientBase.copyFromUrl Method can also be used for this, but it will wait for the copy to complete before returning a response. You could choose what you need.

unknown
  • 6,778
  • 1
  • 5
  • 14
  • how i can move file from specific path for example /test/test.zip from container A to container B.. looks like beginCopy will copy the file it won't perform move operation right? – Mohit Singh Nov 19 '20 at 17:23
  • and container are in same storage account – Mohit Singh Nov 19 '20 at 17:35
  • You need to copy to container B then delete the blob in container A. There is no direct method. There is the [document](https://learn.microsoft.com/en-us/learn/modules/copy-blobs-from-command-line-and-code/7-move-blobs-using-net-storage-client) with .Net, you could refer to the steps of moving files. – unknown Nov 20 '20 at 09:22