请求GET保存不完全文件

时间:2016-01-21 09:59:21

标签: python api

需要使用Artifactory REST API从Artifactory下载artefact。

我的代码是:

with open(jfrog_testfile, 'wb') as outfile:
    data = requests.get(baseurl, auth=(jfrog_user, jfrog_pswd), stream=True)
    shutil.copyfileobj(data.raw, outfile)
    data.raise_for_status()

另一次尝试也有同样的结果:

with open(jfrog_testfile, 'wb') as outfile:
    data = requests.get(baseurl, auth=(jfrog_user, jfrog_pswd), stream=True)
    for chunk in data.iter_content(65536):
        outfile.write(chunk)
    data.raise_for_status()

baseurl是正确的,即:

baseurl = os.path.join(jfrog_url, jfrog_api, jfrog_repo, jfrog_tetstdir, jfrog_testfile)

print(baseurl)

返回:

https://project.artifactoryonline.com/project/api/storage/project-releases-local/ppp/artefact.war

问题是 - 确切的人工制品大小是5.7 MB,但在那之后我只得到了841个字节:

$ ls -l artefact.war 
-rw-r--r--  1 username  staff  841 Jan 21 11:47 artefact.war

即使我使用curl -o运行相同的请求 - 我也有相同的841字节。 使用wget - 我有整个文件,5,7 MB。

m(e)y请求有什么问题?

1 个答案:

答案 0 :(得分:1)

我的错误是GET的网址。

网址来自:

jfrog_url = 'https://project.artifactoryonline.com/project/'
jfrog_api = 'api/storage/'
jfrog_repo = 'project-releases-local'
jfrog_tetstdir = 'artefact'
jfrog_testfile = 'artefact.war'

baseurl = os.path.join(jfrog_url, jfrog_repo, jfrog_tetstdir, jfrog_testfile)

对于GET,请求中不需要插入jfrog_api

所以,而不是:

requests.get('https://project.artifactoryonline.com/project/api/storage/project-releases-local/ppp/artefact.war')

必须使用

requests.get(https://project.artifactoryonline.com/project/project-releases-local/ppp/artefact.war')

我记得,'api/storage/' / PUT请求需要

POST