How to connect to blob storage via windows batch job

Loke 0 Reputation points
2023-07-24T07:53:15.6966667+00:00

Does anyone has any idea if windows batch job can access to azure blob storage to grab a file using MS DOS script?

Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-07-24T07:57:24.35+00:00

  2. Sumarigo-MSFT 47,466 Reputation points Microsoft Employee Moderator
    2023-07-28T07:29:12.2733333+00:00

    @Loke Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    To connect to Blob storage via a Windows batch job, you can use the Azure CLI (Command-Line Interface) or AzCopy utility. These tools allow you to interact with Azure Blob storage from the command line. Here's a step-by-step guide on how to do it using the Azure CLI:

    1. Install Azure CLI: If you don't have Azure CLI installed on your system, download and install it from the official Microsoft Azure CLI website: https://docs.microsoft.com/en-us/cli/azure/install-azure-cli
    2. Login to your Azure account: Open a command prompt or PowerShell window and run the following command to sign in to your Azure account:
    azlogin  
    az account set --subscription <subscription_id_or_name>
    
    1. To connect to Blob storage, you'll need to generate a shared access signature (SAS) token for your storage account or use your storage account key. The SAS token provides secure access to your Blob storage without exposing your account key.
    az storage blob list --account-name <storage_account_name> --account-key <sas_token> --container-name <container_name>
    

    Adding more information to @Olaf Helper response!

    Windows batch job can access Azure Blob storage and grab a file using MS-DOS script (batch script). You can achieve this by using the Azure AzCopy utility, which is a command-line tool designed to copy data to and from Azure Blob storage.

    1. Install Azure AzCopy: https://docs.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-v10
    2. Obtain the Azure Blob storage connection string (SAS)
    3. Write the batch script (grab_file_from_blob.bat)

    Here is an example Windows batch script that uses azcopy to download a file from Azure Blob Storage:

    @echo off
    
    setlocal
    
    set storage_account_name=<your_storage_account_name>
    set container_name=<your_container_name>
    set blob_name=<your_blob_name>
    set destination_path=<your_destination_path>
    
    set source_url=https://%storage_account_name%.blob.core.windows.net/%container_name%/%blob_name%
    
    azcopy copy "%source_url%" "%destination_path%" --recursive
    
    if %errorlevel% equ 0 (
        echo File downloaded successfully.
    ) else (
        echo Error downloading file.
    )
    
    endlocal
    

    Please let us know if you have any further queries. I’m happy to assist you further.


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.