How to Use the Command 'azcopy' (with Examples)
- Windows
- December 17, 2024
AzCopy is a command-line utility designed for efficient data transfers to and from Azure Storage Accounts. It offers robust options for uploading files, copying data, and synchronizing directories between local machines and Azure cloud services. This utility is particularly beneficial for developers and IT administrators who require fast and reliable cloud data management capabilities.
Use case 1: Log in to an Azure Tenant
Code:
azcopy login
Motivation:
The first step when planning to use AzCopy for managing Azure Storage is authentication. Logging into your Azure Tenant grants you the necessary permissions to interact with your cloud resources. This is crucial because security and authorization are cornerstones of cloud operations.
Explanation:
azcopy
: This invokes the AzCopy utility.login
: The login argument initiates the authentication process. It typically opens a web browser prompting the user to enter their Azure credentials.
Example Output:
Upon successful execution, you receive a browser prompt to authenticate, and once completed, a confirmation message appears in your command line indicating a successful login.
Use case 2: Upload a Local File
Code:
azcopy copy 'path\to\source_file' 'https://storage_account_name.blob.core.windows.net/container_name/blob_name'
Motivation:
Uploading a local file to Azure Storage is a common requirement for organizations storing data in the cloud for scalability, backup, and disaster recovery. This use case addresses the need to move critical local data securely and efficiently to the cloud.
Explanation:
azcopy
: Executes the AzCopy tool.copy
: This command initiates the copy operation.'path\to\source_file'
: Represents the file path of the source file on your local machine.'https://storage_account_name.blob.core.windows.net/container_name/blob_name'
: Specifies the destination URL in Azure Storage, wherestorage_account_name
is your account,container_name
is the container within your account, andblob_name
is the name for the file in Azure.
Example Output:
An output showing the progress of the file upload, confirming when the file has been successfully transferred and providing a summary of bytes transferred and elapsed time.
Use case 3: Upload Files with .txt
and .jpg
Extensions
Code:
azcopy copy 'path\to\source_directory' 'https://storage_account_name.blob.core.windows.net/container_name' --include-pattern '*.txt;*.jpg'
Motivation:
Sometimes, it’s necessary to upload only specific types of files from a directory, such as text files (.txt) and images (.jpg), to Azure Storage. This targeted approach minimizes space usage and ensures only relevant data is uploaded, saving time and resources.
Explanation:
azcopy
: Launches the AzCopy utility.copy
: Starts the copy task.'path\to\source_directory'
: The directory path on your local machine.'https://storage_account_name.blob.core.windows.net/container_name'
: The Azure destination URL.--include-pattern '*.txt;*.jpg'
: This filter specifies which file types to include in the operation, using a semicolon to separate multiple patterns.
Example Output:
The console displays information about each relevant file being uploaded, a progress tracker, and a final success message summarizing the upload activity.
Use case 4: Copy a Container Directly Between Two Azure Storage Accounts
Code:
azcopy copy 'https://source_storage_account_name.blob.core.windows.net/container_name' 'https://destination_storage_account_name.blob.core.windows.net/container_name'
Motivation:
This use case is perfect for scenarios where you need to transfer data between two Azure Storage accounts. It is particularly useful for data migration purposes or when consolidating data from multiple accounts into one.
Explanation:
azcopy
: Calls the AzCopy program.copy
: Engages a copy task.'https://source_storage_account_name.blob.core.windows.net/container_name'
: Source URL representing the storage account and container from which data is to be copied.'https://destination_storage_account_name.blob.core.windows.net/container_name'
: Destination URL for the storage account and container to receive the data.
Example Output:
A detailed log of the container copying process, including any errors encountered, successful transfers, and the total number of copied bytes.
Use case 5: Synchronize a Local Directory and Delete Files in the Destination if They No Longer Exist in the Source
Code:
azcopy sync 'path\to\source_directory' 'https://storage_account_name.blob.core.windows.net/container_name' --recursive --delete-destination=true
Motivation:
Synchronization is essential for maintaining consistent data between a local directory and an Azure Storage container. This use case becomes critical when files in the local directory change, requiring updates in the cloud storage to reflect these changes accurately, including deleting obsolete files from the destination to conserve space.
Explanation:
azcopy
: Executes the AzCopy tool.sync
: Performs a synchronization operation.'path\to\source_directory'
: The local directory path containing data to sync.'https://storage_account_name.blob.core.windows.net/container_name'
: Azure Storage container URL.--recursive
: Ensures all files and subdirectories are included.--delete-destination=true
: Deletes files in the destination that do not exist in the source, ensuring exact mirroring.
Example Output:
The terminal will show a sync report covering what changes were made. Deleted, updated, or new files are reported, offering visibility into storage state.
Use case 6: Display Help
Code:
azcopy --help
Motivation:
Understanding the range of functionalities that AzCopy supports is critical for effective usage. The help command provides a comprehensive guide to all available options and arguments, facilitating self-learning of command-line operations.
Explanation:
azcopy
: Initiates the AzCopy program.--help
: Displays the help information with descriptions of commands and options.
Example Output:
The console outputs a detailed guide of AzCopy commands, options, parameters, and their respective descriptions.
Conclusion:
AzCopy is a powerful command-line tool crucial for efficient Azure Storage data management. From basic operations like logging in and file uploads to more complex tasks like synchronizing directories and copying containers across accounts, AzCopy offers a versatile set of features that cater to various cloud data management needs. Understanding and leveraging these use cases can significantly enhance your data handling capabilities within Azure.