Hi everibody,We have a big issue. We are trying to migrate some attachments from an external system to Asset, but we don't find any API to upload a binary. Do you have any suggestions? I'm really in trouble
Hi @Davide Cascapera
Based on Add attachments to Assets in Jira Service Management Cloud | Jira and Jira Service Management | Atlassian Support
its not possible, only from the UI
Note that attachments can't currently be added via the REST API or CSV.
Unless something has changed recently and they haven't document it yet.
not much help, sorry!
Solved: How do I add a file to the attachments section of ...
To add more to @Christos Markoulatos -Relational-
Go through this guide that Thomas gave. It should work for you. Let me know the results!
@Nikola Perisic nice!!! @Davide Cascapera let us know if it works!
Adding to he another comments:
If you need upload the attachments one time, I have a solution for you.
That way will not work if you need synchronise two systems, but it works on demand.
Analyzing the interface upload button to attach files on Assets, using the dev tools, we can find 3 sequence commands:
Is it possible create a script reading a csv file with object id and file path informations to upload many files in one execution.
In bellow the simple bash script that I created when I was testing it:
#!/usr/bin/env bashset -euo pipefail# 🔧 ConfigurationsWORKSPACE="afd1a" # workspaceIdOBJECT_ID="16" #objectIdJIRA_URL="https://yoursite.atlassian.net"CRED_URL="${JIRA_URL}/gateway/api/jsm/assets/workspace/${WORKSPACE}/v1/attachments/object/${OBJECT_ID}/credentials"FILENAME="anexo.png" # filenameLOCAL_FILE="anexo.png" # local filepath# 1️⃣ get temporary credentialsecho "[INFO] Getting temporary credentials..."RESPONSE=$(curl -s "$CRED_URL" \ -H "accept: application/json" \ -H "content-type: application/json" \ -b 'ajs_anonymous_id=%22d47c5516-1047-48d7...') # put the cookie found via devtools here# Extrair dadosCLIENT_ID=$(echo "$RESPONSE" | jq -r '.clientId')TOKEN=$(echo "$RESPONSE" | jq -r '.mediaJwtToken')MEDIA_BASE=$(echo "$RESPONSE" | jq -r '.mediaBaseUrl')echo "[INFO] Client ID: $CLIENT_ID"echo "[INFO] Token retrieved."# 2️⃣ Upload file to Media Serviceecho "[INFO] Uploading $LOCAL_FILE to Jira Media Service..."UPLOAD_URL="${MEDIA_BASE}/file/binary?name=${FILENAME}"UPLOAD_RESPONSE=$(curl -s -X POST "$UPLOAD_URL" \ -H "Authorization: Bearer $TOKEN" \ -H "Content-Type: image/png" \ -H "x-client-id: $CLIENT_ID" \ --data-binary "@${LOCAL_FILE}")echo "$UPLOAD_RESPONSE" | jq .MEDIA_ID=$(echo "$UPLOAD_RESPONSE" | jq -r '.data.id')if [[ "$MEDIA_ID" == "null" ]]; then echo "[ERROR] Unable to retrieve mediaId from upload!" exit 1fiecho "[INFO] Upload completed, mediaId: $MEDIA_ID"# 3️⃣ Associate file to Asset objectecho "[INFO] Associating file to object $OBJECT_ID in Jira Assets..."ATTACH_URL="${JIRA_URL}/gateway/api/jsm/assets/workspace/${WORKSPACE}/v1/attachments/object/${OBJECT_ID}"SIZE=$(stat -f%z "$LOCAL_FILE" 2>/dev/null || stat -c%s "$LOCAL_FILE") # put the cookie found via devtools herecurl -s -X POST "$ATTACH_URL" \ -H "accept: application/json" \ -H "content-type: application/json" \ -b 'ajs_anonymous_id=%22d47c5516-1047-48d7...' \ --data-raw "{ \"attachments\": [ { \"contentType\": \"image/png\", \"filename\": \"${FILENAME}\", \"mediaId\": \"${MEDIA_ID}\", \"size\": ${SIZE}, \"comment\": \"\" } ] }" | jq .echo "[INFO] Attachment successfully associated!"
Hi @Nikola Perisic and Hi @Christos Markoulatos -Relational- ,
Thank you for sharing. We had already consulted your solution, and at the moment, we are using the approach you suggested. The problem is that in the second call, we cannot understand why the token that is generated seems to last only 60 minutes.Firstly, we don't understand who generates this token, and secondly, how to increase its duration. To give you an idea of the scale, we are migrating attachments from an external CMDB to assets, and we are talking about hundreds of thousands of files of different sizes. It is unthinkable to have to change tokens every 60 minutes.
Kind Regards
Davide
Hi @Davide Cascapera,
You can create a script to retrieve attachments and send them to Assets.
In this script, you could refresh the JWT token after a certain number of attachments have been sent. For example, you can renew it once every 10,000 attachments inside the loop.
It looks like you're new here. Sign in or register to get started.