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?
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.
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 :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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')) ]
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I couldn't get this to work, do you have any more information or documentation?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Must be part of the multipart form data I guess. To be honest I used postman to generate the code, selecting several files.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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=(",", ": ")))
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.