This article describes how to copy all Azure Storage Queues data between two different storage accounts.
For this, we will use Azure Storage SDK for Python to copy all queues (and the respective data) from one Azure Storage Queue to another Azure Storage Queue. This approach will keep the data in the source queues, and will create new queues with the respective data in the destination Azure Storage Queue.
This script was developed and tested using the following versions but it is expected to work with previous versions:
In this section, you can find a sample code to copy all queues data between two Storage Accounts using the Azure Storage SDK for Python.
This Python sample code is based on Azure Storage SDK for Python. Please review our documentation here Quickstart: Azure Queue Storage client library for Python.
Prerequisites
Download or use any Python IDE of your choice.
pip install azure-identity
pip install azure-storage-queue
Please see below the sample code to copy all the queues data between two Azure Storage Accounts using the storage connection string.
Special note: Only queues that do not exist with the same name in the destination Storage Account will be copied.
import os
from azure.identity import DefaultAzureCredential
from azure.storage.queue import QueueServiceClient, QueueClient, QueueMessage
source_connection_string = "X"
target_connection_string = "X"
# Create a QueueServiceClient for both source and target account)s
source_client = QueueServiceClient.from_connection_string(source_connection_string)
target_client = QueueServiceClient.from_connection_string(target_connection_string)
# List all queues from the source account
for queue in source_client.list_queues():
try:
if (target_client.get_queue_client(queue.name) != queue.name):
# Create the same queue in the target account
target_queue_client = target_client.create_queue(queue.name)
# Read messages from the source queue
for message in source_client.get_queue_client(queue.name).receive_messages():
# Add the message to the target queue
target_queue_client.send_message(message.content)
print(f"This '{queue.name}' queue was copied successfully.")
except Exception as ex:
if 'QUEUE_ALREADY_EXISTS' in str(ex):
print(f"This '{queue.name}' queue was not copied because the target Storage Account alreday have a queue with this name.")
else:
print(ex)
After executing this sample code, it is expected that you will find all the queues from the source Storage Account in the destination Storage Account, as well as the data/messages from those queues.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.