Search code examples
bashazure-blob-storageazure-virtual-machineazure-managed-identity

Uploading a file to azure storage account as a blob with authentication using managed identities


I want to upload a text file from a VM to storage account. For this I have created a VM with system assigned managed identity enabled. I assigned the managed identity to Storage account as RBAC policy (given the Storage Blob Data Owner permissions). I now want to upload the file in a container name log using curl request (since I don't have az cli installed in the VM). How can I do that.

The file that I want to upload is test.txt and want to upload with same name.

Thanks


Solution

  • Uploading a file to azure storage account as a blob with authentication using managed identities.

    There are several ways to upload files to a storage account.

    Method 1:

    Upload a file to a storage account using a SAS token with the 'curl' command.

    1. Go to the storage account and generate a SAS token with the required permissions

    enter image description here

    1. Run the following command to copy the file from the Linux VM to the storage account using curl..
      curl -H "x-ms-blob-type: BlockBlob" --upload-file samplefile.txt --url "https://venkat12.blob.core.windows.net/log/samplefile.txt?<SAS_Token>"
    

    Output:

    enter image description here

    The file has been uploaded to the storage account.

    enter image description here

    Method 2: : Azcopy with managed Identity

    Download AzCopy:

    wget https://aka.ms/downloadazcopy-v10-linux
    

    Extract the AzCopy

    tar -xvf downloadazcopy-v10-linux
    

    Move the AzCopy to a Directory

    sudo cp ./azcopy_linux_amd64_*/azcopy /usr/local/bin/
    

    Verify the Installation:

    azcopy --version
    

    Login : sudo azcopy login --identity

    Execute the command below to upload the files to the storage account.

    sudo azcopy  copy '/home/Venkat/samplefile.txt' 'https://venkat12.blob.core.windows.net/log/samplefile.txt'
    

    Output:

    enter image description here