Bulk import attachments to Jira server issues via REST API

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

目的

If the project you're importing attachments from ever had its project key changed, the backup files will contain the old project key and this KB is not going to work without changes.

Some data will not be imported

This import does not include metadata about the attachment, such as: author, created date, etc. If this is important do not use this KB.


In several situations, you may need to import several attachments to several JIRA issues. One example is if you've imported issues from another JIRA using CSV, but was unable to add the attachments to the CSV. If the issue keys were not modified (you included them on the import), you can add the attachments this way.

You can attach a single file in a single JIRA issue using REST API. However, as the number of attachments grow, this will become harder and more error prone.

You'll find here an automated way to import many attachments to many JIRA issues with a simple click of a button (or almost).

It's important to point out that these steps were only tested in JIRA 6.4 and may not work in other versions. Please, test it first.

前提条件

To use the following steps, make sure the following prerequisites are met (otherwise, it will simply not work).

  1. You have access to a user who can add the attachments to all issues
  2. You have a computer with access to your JIRA and it's running Linux or Mac. The OS running the JIRA Server doesn't matter, but you'll have to run a bash script that won't work on Windows
  3. You have all the attachments in the following directory structure (the one used by JIRA):
  • ABC
    • ABC-1
      • attachment1.png
      • attachment2.jpg
    • ABC-2
      • attachment3.pdf

Where ABC is the project key and ABC-1 and ABC-2 are issue keys in this project.

Each attachment must be within a directory with the issue key the attachment will be added to, which in its turn must be within a directory with the project key.\

ソリューション

Fixing File Names (only if exporting from another JIRA)

If you're importing this from another bug tracker and the filenames are correct, skip this section and go directly to the 'Procedure' section.

This directory structure was not chosen randomly. It's exactly the one used by JIRA to hold the attachments, so you can use this easily to migrate to another JIRA, simply using the attachments in JIRA's 'attachments' directory.

However, if this is the case, JIRA doesn't keep the files with their names, it keeps the files with filenames as their IDs in the database. JIRA keeps the original file name in the database.

If you go directly to the 'Procedure' section, the attachments will be imported with the file ID as their name. Also, they will have other ID as their filename in the destination directories: a mess.

Follow these instructions first to change the file names to their correct ones.

前提条件

  • Access to the source JIRA's DB
  • Access to the source JIRA's attachments

Be aware that this was only tested in Postgres database.

手順説明

  1. Find the project directory (ABC) under the 'attachments' folder
  2. Copy the directory elsewhere (we will only change the copy, not to affect the source JIRA Server)
    1. If you're using a Windows JIRA Server, you'll need to copy the attachments to a Linux or Mac machine.

      If you make any of the following changes to the actual JIRA attachments directory instead of a copy, this will make your JIRA Server no longer recognize the attachments.

  3. Run the following select in the source JIRA Server database to generate a few commands

    If you're importing from a Jira Cloud instance then contact Atlassian Support to run the query below.

    select ( 'mv ' || project.originalkey || '-' || jiraissue.issuenum || '/' || fileattachment.id || ' "' || project.originalkey || '-' || jiraissue.issuenum || '/' || filename || '"' ) as command from fileattachment join jiraissue on fileattachment.issueid = jiraissue.id join project on jiraissue.project = project.id where project.pkey = 'ABC'; 


    1. Replace 'ABC' by the project key, of course

    2. This query contains two backslashes used to space the double quotation marks. Depending on the database and version, it may need to be removed, replacing <\"> by <">
  4. Navigate to the copy project directory in the Linux/Mac server
  5. Run the 'mv' commands generated by the select
  6. Remove the 'thumbs' directory from the copy directory as we don't want to also add the thumbnails.
cd ABC
rm -rf ./ABC-*/thumbs


方法

Follow these instructions to bulk add all attachments to all issues at once. This can only be done to a single project, but you can run it more than once if you need to add to more.

If you followed the instructions in 'Fixing Files Names', please use the directory with the fixed attachments.

  1. Create an API token here.

  2. Navigate to the project directory

    cd ABC
  3. Download jira_attachments_import.sh to the project directory (ABC, for example)
  4. Give the file executable permissions

    chmod a+x jira_attachments_import.sh
  5. Run it 

    ./jira_attachments_import.sh <email address> <API token> ABC https://charliesjira.atlassian.net
    1. Enter your email address, API token, project key (it doesn't retrieve it from the directory yet) and JIRA URL (https://charliesjira.atlassian.net)
       
  6. The attachments will be imported one by one. You can watch for errors.


The expected return for each attachment if it's successful:

HTTP/1.1 200 OK

説明

You can attach a single file in a single JIRA issue using REST API. However, as the number of attachments grows, this will become harder and more error-prone.

You'll find here an automated way to import many attachments to many JIRA issues with a simple click of a button (or almost).

製品Jira
プラットフォームサーバー
最終更新日 2019 年 9 月 25 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.