Search code examples
azureazure-blob-storageazure-storageazure-storage-emulatorazurite

using multiple storage accounts locally using azurite at the same time


I'm using azurite and azure storage explorer for local development. I have the devstoreaccount1 account by default with key Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw== and the blob,queue,table running on port 10000,10001,10002.

Now I want to test moving a file from one storage account to another storage account. The code for doing that I already have but I need some help to create another storage account running in parallel with devstoreaccount1

I tried to do something like this in a command prompt. It was mentioned in this

set AZURITE_ACCOUNTS="devstoreaccount1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;account1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="

but it didn't work. I only see the devstoreaccount1 in azurite. Also when I close the command prompt the env I set is lost when I do a echo

Can someone give me a step by step approach. I couldnt find any video or proper step by step walkthrough


Solution

  • Now I want to test moving a file from one storage account to another storage account. The code for doing that I already have but I need some help to create another storage account running in parallel with devstoreaccount1.

    In my environment, I executed the below command to have 2 accounts in 1 Azurite instance.

    Command:

    C:\Windows\System32>setx AZURITE_ACCOUNTS "devstoreaccount1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;account1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
    
    SUCCESS: Specified value was saved.
    
    C:\Windows\System32>echo %AZURITE_ACCOUNTS%
    devstoreaccount1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;account1:Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==
    

    And I'm running the Azurite.

    C:\Windows\System32>azurite --silent
    Azurite Blob service is starting at http://127.0.0.1:10000
    Azurite Blob service is successfully listening at http://127.0.0.1:10000
    Azurite Queue service is starting at http://127.0.0.1:10001
    Azurite Queue service is successfully listening at http://127.0.0.1:10001
    Azurite Table service is starting at http://127.0.0.1:10002
    Azurite Table service is successfully listening at http://127.0.0.1:10002
    

    Now, Verify Accounts in Azure Storage Explorer:

    Connect with Local storage emulator

    Field Value
    Display Name local-1
    Account Name devstoreaccount1
    Account Key Eby8vdM02xNOcqFlqUwJPLlmEtlCxxxx==
    Blobs Port 10000
    Files Port (Leave empty)
    Queues Port 10001
    Tables Port 10002
    Use HTTPS (Unchecked)

    enter image description here

    Like same I created connection for account1 in my environment.

    Now I using below code i can copy file from one storage account to another storage account.

    Code:

    from azure.storage.blob import BlobServiceClient
    
    # Source (devstoreaccount1) credentials
    source_account_name = "devstoreaccount1"
    source_account_key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
    source_blob_endpoint = f"http://127.0.0.1:10000/{source_account_name}"  # Azurite's default Blob service endpoint for source account
    
    # Destination (account1) credentials
    destination_account_name = "account1"
    destination_account_key = "Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw=="
    destination_blob_endpoint = f"http://127.0.0.1:10000/{destination_account_name}"  # Azurite's default Blob service endpoint for destination account
    
    source_connection_string = f"DefaultEndpointsProtocol=http;AccountName={source_account_name};AccountKey={source_account_key};BlobEndpoint={source_blob_endpoint}"
    destination_connection_string = f"DefaultEndpointsProtocol=http;AccountName={destination_account_name};AccountKey={destination_account_key};BlobEndpoint={destination_blob_endpoint}"
    
    source_blob_service_client = BlobServiceClient.from_connection_string(source_connection_string)
    destination_blob_service_client = BlobServiceClient.from_connection_string(destination_connection_string)
    
    source_container_name = "mycontainer"  # Replace with your source container name
    destination_container_name = "testcontainer"  # Replace with your destination container name (can be same as source)
    
    # Source blob name to copy
    source_blob_name = "seed.txt"  # Replace with the name of the file you want to copy
    destination_blob_name = "sample.txt"  # The name for the blob in the destination
    
    source_blob_client = source_blob_service_client.get_blob_client(container=source_container_name, blob=source_blob_name)
    
    
    destination_blob_client = destination_blob_service_client.get_blob_client(container=destination_container_name, blob=destination_blob_name)
    
    with open("temp_blob.txt", "wb") as temp_file:
       temp_file.write(source_blob_client.download_blob().readall())
    
    
    with open("temp_blob.txt", "rb") as data:
       destination_blob_client.upload_blob(data)
    
    print(f"Blob '{source_blob_name}' copied from '{source_container_name}' to '{destination_container_name}'.")
    

    Output:

    Blob 'seed.txt' copied from 'mycontainer' to 'testcontainer'.
    

    enter image description here

    Microsoft Azure Storage Explorer:

    enter image description here

    Reference:

    GitHub - Azure/Azurite: A lightweight server clone of Azure Storage that simulates most of the commands supported by it with minimal dependencies