Jira Rest API通过Python获取问题附件

时间:2019-08-29 13:00:38

标签: python-3.x jira jira-rest-api

因此,我需要使用python在Jira中下载该问题的附件。我有下一个代码

from atlassian import Jira

issue = jira.issue(issuekey, fields='summary,comment,attachment')

for attachment in issue['fields']['attachment']:
with open((attachment.filename), 'wb') as file:
    file.write(attachment.get(b'', b''))

运行代码后,我得到3个空文件(txt,png,png),里面没有任何数据。

如何将文件从发行版下载到当前文件夹?

2 个答案:

答案 0 :(得分:1)

尝试使用expand="attachment"

例如:

issue = jira.issue(issuekey,  expand="attachment")

for attachment in issue['fields']['attachment']:
    with open(attachment.filename, 'wb') as file:
        file.write(attachment.get())

答案 1 :(得分:1)

您需要链接到附件内容的链接,该附件存储在键“内容”下。然后,只需使用Jira库中的.get()请求即可

for attachment in issue['fields']['attachment']:
    link = attachment['content']
    link = link.split("https://jira.companyname.com/")[1]
    b_str = jira.get(link, not_json_response=True)
    with open((attachment['filename']), 'wb') as file:
        file.write(b_str)

请注意,您需要修剪链接,因为jira.get()会自动将域包含到请求url中。