How to add index tags and it's values while uploading blobs using azcopy?

Rohit Singh 0 Reputation points
2025-03-19T23:29:22.1166667+00:00

How to add index tags and it's values while uploading blobs using azcopy?

I want to upload thousands of blobs in a storage account using azcopy tool. I want to also add index tags to each blob with it's values. For example, for each blob that i upload i want to add 2 index tags "legacylastcreated" & "legacylastmodified" and it's values respectively. Offcourse the values will be different for these tags for each blob uploaded. Please help me if azcopy can do this or what is the best way to achieve this?

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

2 answers

Sort by: Most helpful
  1. Nandamuri Pranay Teja 3,610 Reputation points Microsoft External Staff Moderator
    2025-03-20T02:04:33.1433333+00:00

    Hello Rohit

    I understand that you require the upload of thousands of blobs, each associated with distinct index tag values for legacylastcreated and legacylastmodified.

    Please be informed that AZ Copy is capable of adding index tags while uploading blobs, making it a highly effective method for managing this process. You can utilize the --blob-tags parameter to define the index tags along with their corresponding values.

    The --blob-tags parameter allows for a list of key-value pairs separated by semicolons, formatted as key=value. In your case, it would appear as follows: **--blob-tags="legacylastcreated=2025-10-26T10:00:00Z;legacylastmodified=2025-10-26T12:00:00Z"**You can use either PowerShell or Bash in conjunction with Azure CLI to manage the process. The script will loop through each file you want to upload and within the loop, the script will retrieve the values of legacylastcreated and legacylastmodified, which are derived from the timestamps of the file. The script will then use AZ Copy to upload the file, including the unique tag values.

    Please be informed that Your SAS token needs to possess permissions for both writing blobs and setting tags. Utilize --permissions rw,t (or add t for tags). The yyyy-MM-ddTHH:mm:ssZ format is crucial for consistent tag values. If it's for your production use, add error checking to your script because the AZ Copy is already optimized; however, the script introduces some additional overhead. For very large uploads, it is advisable to implement optimization strategies within your scripting.

    Bash script:

    #!/bin/bash
    # Set your variables sourcePath="/path/to/your/source/folder" 
    # Replace storageAccountName="<your-storage-account-name>" 
    # Replace containerName="<your-container-name>" 
    # Replace sasToken="<YourSASToken>" 
    # Replace with a SAS token with 'write' and 'tag' permissions # Construct the destination URL destinationUrl="https://$storageAccountName.blob.core.windows.net/$containerName" 
    # Loop through the files for file in "$sourcePath"/*; do if [ -f "$file"; then 
    # Get the file's creation and last modification times in UTC lastCreated=$(date -u -r "$file" +"%Y-%m-%dT%H:%M:%SZ") lastModified=$(date -u -r "$file" +"%Y-%m-%dT%H:%M:%SZ") # Construct the tag string tags="legacylastcreated=$lastCreated&legacylastmodified=$lastModified" 
    # Upload the file with tags using AzCopy azcopy copy \ "$file" \ "$destinationUrl/$(basename "$file")" \ --blob-tags="$tags" \ --log-level=INFO 
    # Optional: For logging echo "Uploaded $(basename "$file") with tags: $tags" 
    # Optional: Print progress fi done echo "Upload complete."
    

    PowerShell script:

    Set your variables $sourcePath = "C:\Your\Source\Folder" 
    # Replace with your folder $storageAccountName = "<your-storage-account-name>" 
    # Replace $containerName = "<your-container-name>" # Replace $sasToken = "<YourSASToken>" # Replace with a SAS token with 'write' and 'tag' permissions 
    # Construct the destination URL $destinationUrl = "https://$storageAccountName.blob.core.windows.net/$containerName" 
    # Get the files $files = Get-ChildItem -Path $sourcePath -File 
    # Loop through the files foreach ($file in $files) { # Get the file's creation and last write times in UTC format $lastCreated = $file.CreationTimeUtc.ToString("yyyy-MM-ddTHH:mm:ssZ") $lastModified = $file.LastWriteTimeUtc.ToString("yyyy-MM-ddTHH:mm:ssZ") # Construct the tag string $tags = "legacylastcreated=$lastCreated&legacylastmodified=$lastModified" 
    # Upload the file with tags using AzCopy azcopy copy "$($file.FullName)" "$destinationUrl/$($file.Name)" --blob-tags="$tags" --log-level=INFO 
    # Optional: For logging Write-Host "Uploaded $($file.Name) with tags: $($tags)" 
    # Optional: Print progress } Write-Host "Upload complete."
    

    Note- If you don't need to use the tags for searching or filtering blobs, consider using metadata instead. https://learn.microsoft.com/en-us/azure/storage/common/storage-use-azcopy-blobs-properties-metadata

    Hope the above answer helps! Please let us know do you have any further queries.


    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. 


  2. Venkatesan S 2,820 Reputation points Microsoft External Staff Moderator
    2025-03-20T14:05:10.5966667+00:00

    Hi @Rohit Singh

    To add an index tag to blobs, it is important to ensure that the Azure SAS token includes the necessary permissions. Please follow these steps:

    1. Navigate to the Azure Portal and open your Storage Account.
    2. Go to Shared Access Signature (SAS).
    3. Select Blob under Allowed Services.
    4. Under Allowed Resource Types, check both Container and Object.
    5. Set the Allowed Permissions to Read, Write, and List.
    6. Enable Allowed Blob Index Permissions, ensuring Read/Write, and Filter are selected.
    7. Specify the Start and Expiry Time as per your requirements.
    8. Choose the appropriate Allowed Protocols like Https only or Https and Http.
    9. Click Generate SAS and Connection String.

    Portal:enter image description here

    Now, using the above SAS token with the following script, I am able to upload a file with an index tag to Azure Blob Storage.

    Script:

    $sastoken="?sv=2024-11-04&ss=b&srt=co&sp=rwltf&se=2025-03-20T21:24:12Z&st=2025-03-20T13:24:12Z&spr=https&sig=redacted"
    $storageUrl = "https://<storage account name>.blob.core.windows.net/<container name>"
    
    $sourcePath = "Your local path"
    $files = Get-ChildItem -Path $sourcePath
    
    $uploadedFiles = @()
    
    foreach ($file in $files) {
        $localFilePath = "$sourcePath$($file.Name)"
        $blobUrl = "$storageUrl/$($file.Name)$sastoken"
        
        # Format timestamps correctly (Azure supports ISO 8601)
        $created = (Get-Date).ToUniversalTime().ToString("o") 
        $modified = (Get-Date).AddMinutes(10).ToUniversalTime().ToString("o")
    
        # Define blob tags (must be URL-encoded and use '&' separator)
        $blobTags = "legacylastcreated=$($created -replace ':', '%3A')&legacylastmodified=$($modified -replace ':', '%3A')"
    
        azcopy copy "$localFilePath" "$blobUrl" --blob-tags="$blobTags"
    
        $uploadedFiles += $file.Name
    }
    
    # Final confirmation message
    if ($uploadedFiles.Count -eq $files.Count) {
        Write-Host "All files uploaded successfully: $($uploadedFiles.Count) files."
    } else {
        Write-Host "Warning: Some files may not have uploaded successfully."
    }
    

    Output:

    All files uploaded successfully: xxxx files.

    Portal:
    enter image description here

    Reference:

    Manage and find Azure Blob data with blob index tags | Microsoft Learn

    Hope this answer helps! 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.

    Update:

    I want to read the tag values from a csv and add that to the blob.

    # Define storage account details
    $sastoken="?sv=2024-11-04&ss=b&srt=co&sp=rwltf&se=2025-03-20T21:24:12Z&st=2025-03-20T13:24:12Z&spr=https&sig=redacted"
    $storageUrl = "https://<storage account name>.blob.core.windows.net/<container name>"
    
    # Path to local files and CSV
    $sourcePath = "C:\Your\Local\Path\"
    $csvFile = "C:\Your\Local\Path\tags.csv"
    
    # Read the CSV file (excluding header)
    $csvData = Import-Csv -Path $csvFile
    
    # Get all files in the directory
    $files = Get-ChildItem -Path $sourcePath -File
    $uploadedFiles = @()
    
    # Ensure file count matches tag count
    if ($files.Count -ne $csvData.Count) {
        Write-Host "Error: The number of files does not match the number of tag entries in CSV."
        exit 1
    }
    
    # Upload files sequentially, applying tags in order
    for ($i = 0; $i -lt $files.Count; $i++) {
        $file = $files[$i]
        $localFilePath = $file.FullName
        $blobUrl = "$storageUrl/$($file.Name)$sastoken"
    
        # Get tag values from the CSV
        $created = $csvData[$i].legacylastcreated
        $modified = $csvData[$i].legacylastmodified
    
        # Define blob tags (URL-encoded)
        $blobTags = "legacylastcreated=$($created -replace ':', '%3A')&legacylastmodified=$($modified -replace ':', '%3A')"
    
        azcopy copy "$localFilePath" "$blobUrl" --blob-tags="$blobTags"
    
        $uploadedFiles += $file.Name
    }
    
    # Final confirmation message
    Write-Host "All files uploaded successfully: $($uploadedFiles.Count) files."
     
    

    The above code will read the tag value from CSV file and add the blob to Azure blob storage.


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.