scala code to copy azure blob from one container to another

In this short article, we will write a program in scala to copy an azure blob from one container to another. In the below code the storageAccountName refers to the Storage Account in the Azure and storageKeyValue refers to the access key to authenticate your application when making requests to this Azure storage account. We can either choose to delete the source blob after copying the blob to the target container or we can keep the source blob as well by setting the deleteBlobAfterCopy to false.

import com.microsoft.azure.storage.CloudStorageAccount
import com.microsoft.azure.storage.blob.CopyStatus

val storageAccountName = "Enter_Your_Storage_Account_Name"
val storageKeyValue = "Enter_Your_Storage_Account_Key"
val deleteBlobAfterCopy=true

//Parses a connection string and returns a cloud storage account created from the connection string

lazy val cloudStorageAccountConnectionString = CloudStorageAccount.parse(s"DefaultEndpointsProtocol=https;AccountName=${storageAccountName};AccountKey=${storageKeyValue};EndpointSuffix=core.windows.net")


/**
* Creates a new Blob service client.
*/

lazy val blobClientInstance = cloudStorageAccountConnectionString.createCloudBlobClient()

/**
* Gets a {@link CloudBlobContainer} object with the specified name.
*/

val srcBlobContainerInstance = blobClientInstance.getContainerReference("Source-Container")

/**
* Returns a reference to a {@link CloudBlockBlob} object that represents a block blob in this container.
*/

val srcBlobReference = srcBlobContainerInstance.getBlockBlobReference("Source-Path/sourceBlob.txt")

val dstBlobContainerInstance = blobClientInstance.getContainerReference("Target-Container")
val dstBlobReference = dstBlobContainerInstance.getBlockBlobReference("Target-Path/targetBlob.txt")

/**
* Requests the service to start copying a block blob's contents, properties, and metadata to a new block blob.
*/

dstBlobReference.startCopy(srcBlobReference)

var checkCopyStatus = CopyStatus.PENDING
while (checkCopyStatus == CopyStatus.PENDING) {
        Thread.sleep(500)
  /**
     * Populates a blob's properties and metadata.
     * <p>
     * This method populates the blob's system properties and user-defined metadata. Before reading or modifying a
     * blob's properties or metadata, call this method or its overload to retrieve the latest values for the blob's
     * properties and metadata from the Microsoft Azure storage service.
     *
     * @throws StorageException
     *             If a storage service error occurred.
     */
  
 dstBlobReference.downloadAttributes()
 checkCopyStatus = dstBlobReference.getCopyState.getStatus
}

 /**
     * Deletes the blob if it exists.
     * <p>
     * A blob that has snapshots cannot be deleted unless the snapshots are also deleted. If a blob has snapshots, use
     * the {@link DeleteSnapshotsOption#DELETE_SNAPSHOTS_ONLY} or {@link DeleteSnapshotsOption#INCLUDE_SNAPSHOTS} value
     * in the <code>deleteSnapshotsOption</code> parameter to specify how the snapshots should be handled when the blob
     * is deleted.
     *
     * @return <code>true</code> if the blob was deleted; otherwise, <code>false</code>.
     *
     * @throws StorageException
     *             If a storage service error occurred.
     *
     */
if(deleteBlobAfterCopy)
        srcBlobReference.deleteIfExists()