简体   繁体   English

在 windows 上使用 python 自动化 azure azcopy

[英]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!我正在尝试使用python中的 azcopy,我已经在CLI中使用过它并且它正在工作!

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现在,我想使用 python 自动执行这些命令,我知道 azcopy 也可以使用 SAS 键使用,但对于我的工作来说,这超出了 scope

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.我已经在我的环境中set AZCOPY_SPA_CLIENT_SECRET

I am using python 3 on windows.我在 windows 上使用 python 3。

Every time I get this error:每次我收到此错误时:

Failed to perform login command: service principal auth requires an application ID, and client secret/certificate无法执行登录命令:服务主体身份验证需要应用程序 ID 和客户端密码/证书

NOTE: If your credential was created in the last 5 minutes, please wait a few minutes and try again.注意:如果您的凭据是在最近 5 分钟内创建的,请等待几分钟,然后重试。

I don't want to use Azure VM to do this job我不想使用 Azure VM 来完成这项工作

Could anyone please help me fix this problem?谁能帮我解决这个问题?

This is because the set cmd does not set a permanent environment variable, it only takes effect in the current windows cmd prompt.这是因为set cmd 并没有设置永久环境变量,它只在当前 windows cmd 提示下生效。

You should manually set the environment variable via UI or try to use setx command.您应该通过 UI 手动设置环境变量或尝试使用setx命令。

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).我用你的代码做了一个测试,并根据用户界面手动设置了AZCOPY_SPA_CLIENT_SECRET的环境变量,然后代码可以正常运行(可能需要几分钟才能生效)。

Test result is as below:测试结果如下:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM