Hello,
I am trying to create a ScriptRunner post-function where I send the attachment in a ticket to a REST service via POST. According to the documentation of this service, I need to send the binary content of the attachment.
I have seen in the Atlassian documentation (https://docs.atlassian.com/software/jira/docs/api/9.12.2/com/atlassian/jira/issue/attachment/class-use/Attachment.html) that there is a method in the AttachmentManager class (streamAttachmentContent(Attachment attachment, InputStreamConsumer<T> consumer)) which returns the binary content of the attachment, but I don’t know how to handle the second parameter of the InputStreamConsumer. Can anyone give me a hand?
Thanks,
Best regards.
Hi Cristian,
You picked up rigth path! Let me provide you some java method that returns first attachment for issue ABC-1234 in bytes
import groovy.transform.Field;
import org.apache.commons.io.IOUtils;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.AttachmentManager;
import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.component.ComponentAccessor;
@Field final AttachmentManager attachmentManager = ComponentAccessor.getAttachmentManager();
@Field Issue issue = ComponentAccessor.issueManager.getIssueByCurrentKey("ABC-1234")
getAttachmentFileBytes(issue.getAttachments()?.getAt(0))
byte[] getAttachmentFileBytes(Attachment attachment) throws IOException {
return attachmentManager.streamAttachmentContent(
attachment,
inputStream -> {
try (inputStream) {
return IOUtils.toByteArray(inputStream);
}
});
}
Hope it will help you!
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.