I’m working in some automation and one of the things I needed to solve was a script to download JIRA attachments with Python, here’s how I did it.

Using the REST API of JIRA and some python modules:

We start defining the she-bang, author, modules and variables that we’ll need before:

# miguel ortiz
# Requests module: http://docs.python-requests.org/en/latest/
# Documentation: <url>

# -----------------------------------  modules
import sys
import csv, json
import requests

# ---------------------------------- variables
myTicket = sys.argv[1] # Your ticket: ABC-123
user = 'miguel'     # JIRA user
pasw = 'mypassword' # JIRA password
jiraURL = 'https://yourinstance.jira.com/rest/api/latest/issue/'
fileName = 'my_attached_file' # In this case we'll be looking for a specific file in the attachments
attachment_final_url="" # To validate if there are or not attachments

Using ‘requests’ to perform the connection to JIRA:

def main() :
    print '\n\n [ You are checking ticket: ' + myTicket + ' ]\n'
    # Request Json from JIRA API
    r = requests.get('jiraURL+myTicket, auth=(user, pasw),timeout=5)

    # status of the request
    rstatus = r.status_code
    # If the status isn't 200 we leave
    if not rstatus == 200 :
        print 'Error accesing JIRA:' + str(rstatus)
        rlinkdata = rlink.json()
        data = r.json()

Now we validate if there are attachments and if our desired attachment is present and then form the URL of the attachment to be downloaded:

 if not data['fields']['attachment'] :            
     status_attachment_name = 'ERROR: Nothing attached, attach a file named: ' + fileName
        for i in data['fields']['attachment'] :
          if i['filename'] == fileName :
             attachment_final_url = i['content']
             status_attachment_name = 'OK: The desired attachment exists: ' + fileName
             attachment_name = False
             attachment_amount = False
             attachment_files = False
          else :
            attachment_files = False
            status_attachment_name = 'ERROR: None of the files has the desired name ' 
            attachment_name = True
            attachment_amount = True

Once we validated the file is present we download it (I’ve added UTF8 support because of my specific needs):

if attachment_final_url != "" :
        r = requests.get(attachment_final_url, auth=(user, pasw), stream=True)
        with open(fileName, "wb") as f: 

if __name__ == "__main__" :

And that’s it.