Artsom
Artsom

Reputation: 171

Jira rest api get issue attachment via python

So, I need to download attachments to the issue in Jira using python. I have next code

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''))

After running the code I'm getting 3 empty files(txt, png, png) without any data inside..

How can I get(download) files from issue to my current folder?

Upvotes: 1

Views: 5105

Answers (3)

Sumit Gupta
Sumit Gupta

Reputation: 46

  1. Get the attachment details:
  2. get the jira attachment file URL
  3. download the file using Request Module.
  4. Check the file in file list.

            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)

Upvotes: 0

Ivan Popov
Ivan Popov

Reputation: 116

You need the link to the contents of the attachment which is stored under the key 'content'. Then just use .get() request, that is in Jira library:

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)

Notice that you need to trim the link, because jira.get() automatically includes the domain to the request url.

Upvotes: 2

Rakesh
Rakesh

Reputation: 82765

Try using expand="attachment"

Ex:

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

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

Upvotes: 4

Related Questions