Loading...

How to copy all Azure Storage Queue data between two different Storage Accounts with Python

How to copy all Azure Storage Queue data between two different Storage Accounts with Python

Background

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:

  • Python 3.11.7
  • azure-identity (version: 1.15.0)
  • azure-storage-queue (version: 12.9.0)


Approach

 

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.

  • On Python side, we will use the following packages:
    • azure-identity (more information here azure-identity · PyPI). To install, please run:
      pip install azure-identity​
    • azure-storage-queue (more information here azure-storage-queue · PyPI). To install, please run:
      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.

 

Disclaimer:

  • These steps are provided for the purpose of illustration only. 
  • These steps and any related information are provided "as is" without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.
  • We grant You a nonexclusive, royalty-free right to use and modify the Steps and to reproduce and distribute the steps, provided that. You agree:
    • to not use Our name, logo, or trademarks to market Your software product in which the steps are embedded;
    • to include a valid copyright notice on Your software product in which the steps are embedded; and
    • to indemnify, hold harmless, and defend Us and Our suppliers from and against any claims or lawsuits, including attorneys’ fees, that arise or result from the use or distribution of steps.

 

 

Published on:

Learn more
Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

PostgreSQL for your AI app's backend | Azure Database for PostgreSQL Flexible Server

Use Postgres as a managed service on Azure. As you build generative AI apps, explore advantages of Azure Database for Postgres Flexible Server...

2 hours ago

Storage migration: Combine Azure Storage Mover and Azure Data Box

Migrating storage from on-premises can be challenging. That’s why we are on a mission to make your migrations as simple as possible. We've dev...

4 hours ago

Loop DDoS Attacks: Understanding the Threat and Azure's Defense

In the realm of cybersecurity, Distributed Denial-of-Service (DDoS) attacks are a significant concern. The recent holiday season has unveiled ...

12 hours ago

Azure Communication Services at Microsoft Build 2024

Join us in-person in Seattle or virtually for Microsoft Build 2024 from May 21 to 24. We're excited to share the latest updates from Azure Com...

13 hours ago

Azure Developer CLI (azd) – May 2024 Release

This post announces the May release of the Azure Developer CLI (`azd`), which includes a new demo mode, various .NET Aspire enhancements, and ...

13 hours ago

Join us at Build 2024: Get the latest on Azure Cosmos DB in Seattle or online!

Are you ready to dive into the future of AI and data innovation? Microsoft Build 2024 is just around the corner, taking place May 21–23 in Sea...

15 hours ago

Announcing Data API builder General Availability for Azure Cosmos DB

Great news for developers out there! The Data API Builder for Azure Cosmos DB is now officially available, eliminating the need for writing co...

1 day ago

Organizing rule collections and rule collection groups in Azure Firewall Policy

Firewall Policy is the recommended method to manage Azure Firewall security and operational configurations. When using Firewall Policy, any ru...

1 day ago

Get Started with Azure AI Services | Open AI and Deployment Models

Table of Contents   Overview - Azure AI Services Kind of Azure AI Services Responsible AI Services Limited Access Features Cognitive Acc...

1 day ago
Stay up to date with latest Microsoft Dynamics 365 and Power Platform news!
* Yes, I agree to the privacy policy