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

3vpjnl9f  于 2023-01-12  发布在  Python
关注(0)|答案(2)|浏览(153)

我必须运行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!")

现在我可以看到我的包下载,但我的压缩包是无效。当然与其他一些工具一样curl.exe相同的作品。所以肯定我错过了一些东西在python脚本,但不能确定我错过了什么(下载作品,但包是无效的)。
多谢了!

ivqmmu1c

ivqmmu1c1#

这里有一个更接近原始代码的答案,包括使用最小内存的分块,它只是将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!")
cxfofazt

cxfofazt2#

你一次只传输几个字节,然后把每个块都写入文件,但每次都是新的,所以我怀疑你看到的只是文件中的最后一个块。除非文件非常大,否则你应该可以简单地把整个文件加载到内存中,然后再把它写出来。下面是修改后的版本:

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的测试文件测试了一下,我在网上找到了这个测试文件,它下载得很好。
不再需要块大小,因为您不是在块中下载。:)

相关问题