Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,559,427
Community Members
 
Community Events
184
Community Groups

Attachment upload from Instance A to Instance B

Edited

Hi,

I have two JIRA instances located in two different data-centers.

I am trying to develop a functionality via workflow post function that would copy attachments from issue in Instance A to an issue in Instance B.

I have scriptrunner in both of my JIRA instances.

For now I am just trying to get one attachment via API and upload it to an issue in the same instance via API.

The code below creates the attachment (I am running it from ScriptRunner script console) - however I am only able to upload text attachments.

def connection = new URL("https://xxxx/secure/attachment/10108/test.txt").openConnection() as HttpURLConnection
connection.setRequestMethod( "GET" )
connection.setRequestProperty( "Authorization", "Basic xxxxxxxxxxx" )
connection.doOutput = true
connection.setRequestProperty("Content-Type", "application/json;charset=UTF-8")
connection.connect()
def image = connection.inputStream.text

def body = "---\r\nContent-Disposition: form-data; name=\"file\"; filename=\"text.txt\"\r\nContent-Type: text/plain\r\n\r\n $image \r\n---"

def upConnection = new URL("https://xxxxxxxxxxxxx/rest/api/latest/issue/xxxxxxxxxx/attachments").openConnection() as HttpURLConnection
upConnection.setRequestMethod( "POST" )
upConnection.setRequestProperty( "Authorization", "Basic xxxxxxxxx" )
upConnection.setRequestProperty( "X-Atlassian-Token", "nocheck" )
upConnection.setRequestProperty( "Content-Type", "multipart/form-data; boundary=-")
upConnection.doOutput = true
upConnection.getOutputStream().write(body.getBytes())
upConnection.connect()
def upCode = upConnection.getResponseCode();
def upResult = upConnection.inputStream

upResult.text

 

1 answer

0 votes
mogavenasan
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.
Jan 18, 2021

Hi @Kaspars Naumovs,

Not sure if this is the cause, but you have defined Content-Type twice in the code - line 9 and line 15.

I hope that this helps.

Thanks,
Moga

Thanks for the reply.

But if I remove the Content-Type from line 9 - the file format is unrecognized on JIRA side after import.

If i remove Content-Type from line 15 I get HTTP error 415. 

mogavenasan
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.
Jan 19, 2021

Have you tried to change the Content-Type on line 9 to cater to different file types? For example, let's say you would like to attach a PNG image, then the Content-Type should be "image/png". 

I have tried image/png for png files - however they are unreadable after upload.

I believe the issue is within how I store the source image into a variable.

mogavenasan
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.
Jan 20, 2021

Hi @Kaspars Naumovs,

You should download the file to the filesystem. Can you store the source image on the filesystem and then try to open the file?

I am not sure how would I save a file from the ScriptRunner console / post function on the server filesystem and then access it.

How would this be different then getting the file from the api call?

mogavenasan
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.
Jan 22, 2021

I'm not really an expert on the inputStream and how to pass the data while preserving the file type.

Scriptrunner allows you to place .groovy scripts under <jira-home>/scripts. With this, you can save a tmp file on the same directory for the attachment. For the saving the file, you can save the inputStream into a tmp file; Java – Write an InputStream to a File

I believe Script Console and workflow post-function allows you to choose a script from the filesystem rather than providing inline scripts.

@Kaspars Naumovs Did you manage to find a solution for uploading non-txt files using Rest API from the script runner console?

Hi,

I did not find the solution with the scriptrunner.

As a workaround I am using a scheduled powershell script that downloads and uploads the attachments.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events