Python GET Rest API -包已下载,但我无法打开它(无效)



我必须运行python以以下语法从存储库中获取一些工件(从批处理中调用其变量),因此这部分传递参数是不可更改的。

python get_artifacts.py %USERNAME%:%PASSWORD% http://url/artifactory/package.zip
我的python脚本如下:
import sys
import requests
from requests.auth import HTTPBasicAuth
def get_artifact(url, save_artifact_name, username, password, chunk_size=128):
try:
get_method = requests.get(url, 
auth = HTTPBasicAuth(username, password), stream=True)
with open(save_artifact_name, 'wb') as artifact:
for chunk in get_method.iter_content(chunk_size=chunk_size):
artifact.write(chunk)
except requests.exceptions.RequestException as error:
sys.exit(str(error))
if __name__ == '__main__':
username_and_password = sys.argv[1].split(':')
username = username_and_password[0]
password = username_and_password[1]
url = sys.argv[2]
save_artifact_name = url.split("/")[-1]
print(f'Retrieving artifact {save_artifact_name}...')
get_artifact(url, save_artifact_name, username, password)
print("Finished successfully!")

现在我可以看到我的包下载,但我的zip包是无效.当然可以使用其他工具,比如curl.exe同样有效。所以我肯定在python脚本中缺少一些东西,但无法确定我缺少什么(下载工作,但包无效)。

非常感谢!

这是一个更接近原始的答案,包括将在最小内存下工作的分块。它只是将open()放在下载代码之前:

import sys
import requests
from requests.auth import HTTPBasicAuth
def get_artifact(url, save_artifact_name, username, password, chunk_size=128):
try:
with open(save_artifact_name, 'wb') as artifact:
get_method = requests.get(url,
auth = HTTPBasicAuth(username, password), stream=True)
for chunk in get_method.iter_content(chunk_size=chunk_size):
artifact.write(chunk)
except requests.exceptions.RequestException as error:
sys.exit(str(error))
if __name__ == '__main__':
username_and_password = sys.argv[1].split(':')
username = username_and_password[0]
password = username_and_password[1]
url = sys.argv[2]
save_artifact_name = url.split("/")[-1]
print(f'Retrieving artifact {save_artifact_name}...')
get_artifact(url, save_artifact_name, username, password)
print("Finished successfully!")

您每次流式传输文件几个字节,并将每个块写入文件,但每次都重新写入文件,因此我怀疑您只看到文件中的最后一个块。除非文件非常大,否则您应该能够简单地将整个文件加载到内存中,然后将其写出来。以下是修改后的版本:

import sys
import requests
from requests.auth import HTTPBasicAuth
def get_artifact(url, save_artifact_name, username, password):
try:
get_method = requests.get(url,
auth = HTTPBasicAuth(username, password))
with open(save_artifact_name, 'wb') as artifact:
artifact.write(get_method.content)
except requests.exceptions.RequestException as error:
sys.exit(str(error))
if __name__ == '__main__':
username_and_password = sys.argv[1].split(':')
username = username_and_password[0]
password = username_and_password[1]
url = sys.argv[2]
save_artifact_name = url.split("/")[-1]
print(f'Retrieving artifact {save_artifact_name}...')
get_artifact(url, save_artifact_name, username, password)
print("Finished successfully!")

应该一次获取整个文件并将其写入输出。我刚刚用在网上找到的一个5MB的测试文件进行了测试,它下载得非常好。

不再需要块大小,因为您不再以块下载。:)

相关内容

  • 没有找到相关文章

最新更新