AzCopy: Azure Blob Storage Management Tool
This comprehensive guide provides a quick reference for efficiently interacting with Azure Blob Storage using AzCopy, a powerful command-line utility.
Setup and Authentication
Install AzCopy
To install AzCopy on macOS using Homebrew:
brew install azcopy
For other operating systems, please refer to the official AzCopy installation guide.
Authenticate AzCopy
Before using AzCopy, you need to authenticate:
azcopy login
This command will open a browser window for you to log in to your Azure account. The authentication token is valid for a limited time, so you may need to re-authenticate periodically.
Core Operations
List Blobs
To list blobs in a container:
azcopy list "https://your-storage-account.blob.core.windows.net/your-container"
Sample Output
INFO: Scanning...
INFO: Failed to create OAuth token for authorization. Please use AAD credentials to access the resource.
2023/05/20 15:30:25 Failed to perform list operation
INFO: Listing finished
INFO: Elapsed time: 0.0345 seconds
If you see an authorization error, make sure you've logged in using azcopy login
.
Use the --recursive
flag to list contents of all subdirectories.
Download a Blob
To download a blob from Azure Blob Storage:
azcopy copy "https://your-storage-account.blob.core.windows.net/your-container/path/to/blob.ext" "/path/to/local/destination/"
Sample Output
INFO: Scanning...
INFO: Failed to create OAuth token for authorization. Please use AAD credentials to access the resource.
INFO: Uploading blob...
INFO: Blob upload successful
INFO: Elapsed time: 1.3456 seconds
INFO: Number of transfers completed: 1
INFO: Number of transfers failed: 0
INFO: Final job status: Completed
Upload a File
To upload a file to Azure Blob Storage:
azcopy copy "/path/to/local/file.ext" "https://your-storage-account.blob.core.windows.net/your-container/path/in/container/"
Sample Output
INFO: Scanning...
INFO: Failed to create OAuth token for authorization. Please use AAD credentials to access the resource.
INFO: Uploading blob...
INFO: Blob upload successful
INFO: Elapsed time: 2.3456 seconds
INFO: Number of transfers completed: 1
INFO: Number of transfers failed: 0
INFO: Final job status: Completed
Sync Directories
To sync a local directory with a container:
azcopy sync "/path/to/local/directory" "https://your-storage-account.blob.core.windows.net/your-container" --recursive
Sample Output
INFO: Scanning...
INFO: Failed to create OAuth token for authorization. Please use AAD credentials to access the resource.
INFO: Syncing...
INFO: Sync completed
INFO: Elapsed time: 5.6789 seconds
INFO: Number of transfers completed: 10
INFO: Number of transfers skipped: 5
INFO: Number of transfers failed: 0
INFO: Final job status: Completed
The sync
command can be destructive. Always use --delete-destination=false
(default) to prevent accidental deletions, unless you explicitly want to delete files at the destination that don't exist at the source.
Remove a Blob
To delete a blob from Azure Blob Storage:
azcopy remove "https://your-storage-account.blob.core.windows.net/your-container/path/to/blob.ext"
Sample Output
INFO: Scanning...
INFO: Failed to create OAuth token for authorization. Please use AAD credentials to access the resource.
INFO: Removing blob...
INFO: Blob removal successful
INFO: Elapsed time: 0.5678 seconds
INFO: Number of deletions completed: 1
INFO: Number of deletions failed: 0
INFO: Final job status: Completed
Advanced Usage: SAS Tokens
When working with Shared Access Signature (SAS) tokens, you can use them directly in your AzCopy commands instead of logging in. This is particularly useful for providing limited, temporary access to your storage resources.
List Blobs with SAS Token
azcopy list "https://your-storage-account.blob.core.windows.net/your-container?sv=2020-08-04&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2023-06-01T12:00:00Z&st=2023-05-01T12:00:00Z&spr=https&sig=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Sample Output
INFO: Scanning...
file1.txt
file2.txt
folder1/
folder1/file3.txt
folder2/
folder2/file4.txt
INFO: Listing finished
INFO: Elapsed time: 0.2345 seconds
Download a Blob with SAS Token
azcopy copy "https://your-storage-account.blob.core.windows.net/your-container/path/to/blob.ext?sv=2020-08-04&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2023-06-01T12:00:00Z&st=2023-05-01T12:00:00Z&spr=https&sig=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX" "/path/to/local/destination/"
Sample Output
INFO: Scanning...
INFO: Downloading blob...
INFO: Blob download successful
INFO: Elapsed time: 1.3456 seconds
INFO: Number of transfers completed: 1
INFO: Number of transfers failed: 0
INFO: Final job status: Completed
Upload a File with SAS Token
azcopy copy "/path/to/local/file.ext" "https://your-storage-account.blob.core.windows.net/your-container/path/in/container/?sv=2020-08-04&ss=bfqt&srt=sco&sp=rwdlacupitfx&se=2023-06-01T12:00:00Z&st=2023-05-01T12:00:00Z&spr=https&sig=XXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
Sample Output
INFO: Scanning...
INFO: Uploading blob...
INFO: Blob upload successful
INFO: Elapsed time: 2.3456 seconds
INFO: Number of transfers completed: 1
INFO: Number of transfers failed: 0
INFO: Final job status: Completed
SAS tokens provide direct access to your storage resources. Keep them secure and avoid sharing them publicly. Use short-lived tokens and restrict permissions as much as possible.
Best Practices and Tips
- Use
--recursive
for operations on entire directories. - Simulate actions with
--dry-run
before making changes. - Prevent accidental overwrites with
--overwrite=prompt
. - For large transfers, use
--cap-mbps
to limit bandwidth usage. - Enable logging with
--log-level=INFO
for detailed operation information. - Use
--exclude-pattern
and--include-pattern
for fine-grained control over file selection.