Search code examples
pythonpython-3.xazureazcopy

Automate azure azcopy using python on windows


I am trying to use azcopy from python, I have already used this from CLI and it is working!

I have successfully executted the following commands:

for upload :

set AZCOPY_SPA_CLIENT_SECRET=<my client secret>
azcopy login --service-principal --application-id=<removed> --tenant-id=<removed>
azcopy copy "D:\azure\content" "https://dummyvalue.blob.core.windows.net/container1/result4" --overwrite=prompt --follow-symlinks --recursive --from-to=LocalBlob --blob-type=Detect

Similarly for download

azcopy copy "https://dummyvalue.blob.core.windows.net/container1/result4" "D:\azure\azcopy_windows_amd64_10.4.3\temp\result2" --recursive

Now, I want to automate these commands using python, I know that azcopy can also be used using SAS keys but that is out of scope for my working

First attempt:

from subprocess import call
call(["azcopy", "login", "--service-principal", "--application-id=<removed>", "--tenant-id=<removed>"])

Second attempt:

import os
os.system("azcopy login --service-principal --application-id=<removed> --tenant-id=<removed>")

I have already set AZCOPY_SPA_CLIENT_SECRET in my environment.

I am using python 3 on windows.

Every time I get this error:

Failed to perform login command: service principal auth requires an application ID, and client secret/certificate

NOTE: If your credential was created in the last 5 minutes, please wait a few minutes and try again.

I don't want to use Azure VM to do this job

Could anyone please help me fix this problem?


Solution

  • This is because the set cmd does not set a permanent environment variable, it only takes effect in the current windows cmd prompt.

    You should manually set the environment variable via UI or try to use setx command.

    I did a test by using your code, and manually set the environment variable of AZCOPY_SPA_CLIENT_SECRET as per UI, then the code can run without issues(it may take a few minutes to take effect).

    Test result is as below:

    enter image description here