因此,我需要使用 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),里面没有任何数据..
如何将问题中的文件获取(下载)到我的当前文件夹?
尝试使用
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())
您需要存储在“内容”键下的附件内容的链接。然后只需使用
.get()
请求,该请求位于 Jira 库中:
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()
会自动将域包含到请求网址中。
issue = jira.issue(jira_ticket, expand='changelog')
attach = issue.fields.attachment
file_url = attach[0].content
file_path = "filename"
r = requests.get(file_url, auth=('jira_user', 'jira_pass'))
with open(file_path, 'wb') as f:
f.write(r.content)
对我来说,这是有效的
jira_issue = jira.issue(issue_key, expand="attachment")
for attachment in jira_issue.fields.attachment:
print("Attachment {}".format(attachment))
with open((attachment.filename), 'wb') as file:
file.write(attachment.get(b'', b''))