Downloading attachments via jira module in python

Dickson September 14, 2015

In http://pythonhosted.org/jira/ it states you can get the attachment resource via the call: JIRA.attachment(id)

However, once you get the resource, is there a way to actually download the attachment? 

4 answers

1 accepted

1 vote
Answer accepted
Wlosin
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
February 1, 2019

I used a method as such

from jira import JIRA
jira = JIRA(auth=('<username>','<password>'), options={'server': <URL>})
jira_issue = jira.issue(<issue>, expand="attachment")

for attachment in  jira_issue.fields.attachment  :   
image = attachment.get()    
jira_filename = attachment.filename   
with open(jira_filename, 'wb') as f:       
f.write(image) 

 Other authentication methods also exist, for example I used OAuth. More info on that can be found here.

It's worth noting that I couldn't download files with a space in the attachment's file name using the jira module and had to update my code to use the attachment's .inter_content() attribute and iterate through the content.

 #Write the file to the issue_path directory
with open(jira_filename, 'wb') as f:
#if there is a space in the filename (attachment.get() crashes in this case)
if " " in jira_filename:
for chunk in attachment.iter_content(chunk_size=512):
if chunk: # filter out keep-alive new chunk
f.write(chunk)
else:
#Getting the file from the jira server and writing it to the current directory
image = attachment.get()
f.write(image)
1 vote
hjelev
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
May 11, 2016

You can do it with request like this:

response = requests.get('filepath', auth=('user', 'pass'), stream=True)
with open("attachments/"+filename, 'wb') as out_file:
	shutil.copyfileobj(response.raw, out_file)
del response
0 votes
Darren McElfresh November 10, 2016

Please see the answer here.

0 votes
jrbenito
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
November 27, 2015

The object returnet by jira.attachment(id) method has itself a method called get, did you try it?

Another way is to user the property content to get the JIRA url to the attachment and get it through a http request.

Austin Trombley
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
April 19, 2016

I have tried the same thing, the http route just returns a garbage 30 kb csv file and I couldn't find a GET method for JIRA? 

Suggest an answer

Log in or Sign up to answer