Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Import multiple files into a Jira Cloud issue via Rest API

Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2022

Hello, I wish to upload several files into mutiple Jira issues.

Starting with one: I have followed this procedure https://confluence.atlassian.com/jirakb/how-to-add-an-attachment-to-a-jira-issue-using-rest-api-699957734.html and the more complete one https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-issue-issueidorkey-attachments-post

But those doe not show how to mention several files into the "files" dict.

I am working with Python and the provided snipped and tried:

files = {
'file' : [
("text1.txt", open("text1.txt","rb"), "application-type"),
("text2.txt", open("text2.txt","rb"), "application-type")
]
}

As well as:

files = {
'file1' : ("text1.txt", open("text1.txt","rb"), "application-type"),
'file2' : ("text2.txt", open("text2.txt","rb"), "application-type")
}

But none of those work.
Is there a way to achieve this. I have many files to upload, is the only way to perform this to actually do one api call per file or can we bulk upload them into a single issue?

5 answers

2 accepted

1 vote
Answer accepted
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 11, 2022

Basically, you will need to iterate over a list of files or in this case if you're uploading from a directory. You can to walk through the directory to upload the attachments per issue key or issue keys. However, if these attachments exist on a different Jira platform. You can look into this post and use a similar solution.

Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 12, 2022

So basically that "files" dict only accepts a single file and I therefore need to make one API call per file (that is what I did at the begining, I wanted to know if there was something more efficient).

Thanks :)

0 votes
Answer accepted
Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 23, 2023

It is actually possible to do it using

 

headers = {
    "Content-Type": "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
}

files = [
    ('file', ('file1.txt', open('file1.txt', 'rb'), 'text/plain')),
    ('file', ('file2.jpg', open('file2.jpg', 'rb'), 'image/jpeg')) ]
Melissa Nielsen January 5, 2024

I couldn't get this to work, do you have any more information or documentation? 

Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
January 8, 2024

Must be part of the multipart form data I guess. To be honest I used postman to generate the code, selecting several files.

postman pj curl.png

For the Python code :

import requests

url = "https://xxxx-sandbox-720.atlassian.net/rest/api/3/issue/CAVPIV2-3/attachments"

payload = {}
files=[
('file',('app_access.png',open('/C:/Users/xxx/OneDrive - Klee Group/Images/Logos/app_access.png','rb'),'image/png')),
('file',('image-20230407-082914 (1).png',open('/C:/Users/xxx/Downloads/image-20230407-082914 (1).png','rb'),'image/png'))
]
headers = {
'X-Atlassian-Token': 'no-check',
'Authorization': 'Basic YW50bxxx=='
}

response = requests.request("POST", url, headers=headers, data=payload, files=files)

print(response.text)
2 votes
jyoti12345 March 17, 2023

You will need to iterate over a list of files and need to make an API call per file to upload the attachment to the ticket.

While in a loop if files get dropped, add sleep for a second after each iteration. The sleep duration may vary depending on the performance of jira.

For example, here is code block to upload the attachment to Jira ticket. 

import requests

import time

from requests.auth import HTTPBasicAuth

import json

url = "https://domain.net/rest/api/2/issue/{issueIdOrKey}/attachments"

auth = HTTPBasicAuth("email@example.com", "")

headers = {

"Accept": "application/json",

"X-Atlassian-Token": "no-check"

}

file_names = ['myfile1.txt']

#files should be placed in some location to pick it up. For ex under :/tmp

for file_name in file_names:

   files = {

       "file": (file_name, open("/tmp/"+file_name, "rb"), "application-type")

   }

   response = {}

   try:

      response = requests.post(

          url=url, auth=auth, headers=headers, files=files)

      time.sleep(1) 

   except Exception as err:

       print('Failed to add attachments Error: {}'.format(err)) 

   if response and response.status_code == 200:

       print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ")))

0 votes
jyoti12345 March 17, 2023

You will need to iterate over a list of files and need to make an API call per file to upload the attachment to the ticket.

While in a loop if files get dropped, add sleep for a second after each iteration. The sleep duration may vary depending on the performance of jira.

0 votes
Antoine _Klee Group_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
April 7, 2022

Also how to specify the author and date of import of the attachment?

I guess this is possible since we can specify those infos in a csv import.

https://confluence.atlassian.com/cloudkb/importing-attachments-into-jira-cloud-using-csv-external-system-importer-966669960.html

Suggest an answer

Log in or Sign up to answer