It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

jira-python attached file filename

snuggles Feb 14, 2013

Hi, I'm using jira-python (http://jira-python.readthedocs.org/en/latest/) to attach files to existing issues. The files are being attached ok, but the problem is how Jira is presenting them in the UI.

Here's the code:

from jira.client import JIRA

jira = JIRA(basic_auth=([username], [password]), options={'server': [our_jira_server]})

issue = jira.issue([issue_id])

file = open('/tmp/test2.txt')

attachment_object = jira.add_attachment(issue,file)

The above attaches the file to the issue, but the url to the file in the jira issue is this:

https://[our_jira_server]/jira/secure/attachment/58750/%2Ftmp%2Ftest2.txt

ie: it includes the *path* in the filename used by jira. The above url does not exist on jira, however, if I go here:

https://[our_jira_server]/jira/secure/attachment/58750/test2.txt

The file is there.

Note that this works perfectly:

from jira.client import JIRA

jira = JIRA(basic_auth=([username], [password]), options={'server': [our_jira_server]})

issue = jira.issue([issue_id])

file = open('test.txt')

attachment_object = jira.add_attachment(issue,file)

Since there is no path in the file name, the attachment is displayed in the jira issue like this:

https://[our_jira_server]/jira/secure/attachment/58751/test.txt

I'm not sure what to do. I've tried:

1. finding some way to rename the file before uploading it to JIRA. EG:

file.filename = 'test3.txt'

But filename is readonly attr.

2. tried to change the name of the file once it was uploaded by editing the returned attachment object. EG:

attachment_object.filename = 'test3.txt'

This changes the name in the attachment_object dict in the python session, but doesn't make any change on the jira side.

If you can point me in the right direction that would be very helpful. Thank you!

4 answers

1 accepted

2 votes
Answer accepted
John Welsh Mar 06, 2013

The workaround I used was to call os.chdir() to change the working directory to the folder that contains the file, then file = open('myfilename', 'rb')

snuggles Mar 11, 2013

Sending hugbot 2000 to you. Thank you! I wish I had thought of that.

0 votes
snuggles Feb 19, 2013

It doesn't seem possible that nobody knows how to do this. I'm wondering if I'm either not being clear or if this is the wrong place to ask this question. If you can even just tell me I'm in the wrong place, that would be extremely helpful. I'm not sure what the best place to get help on the jira-python module is... Thanks!

0 votes
Jean-Martin Archer Mar 14, 2013

I changed client.py (so you can pass a filename):

def add_attachment(self, issue, filename, attachment):
        """
        Attach an attachment to an issue and returns a Resource for it.

        The client will *not* attempt to open or validate the attachment; it expects a file-like object to be ready
        for its use. The user is still responsible for tidying up (e.g., closing the file, killing the socket, etc.)

        :param issue: the issue to attach the attachment to
        :param attachment: file-like object to attach to the issue
        :rtype: an Attachment Resource
        """
        # TODO: Support attaching multiple files at once?
        url = self._get_url('issue/' + issue + '/attachments')
        files = {
            'file': (filename,attachment)
        }
        r = self._session.post(url, files=files, headers={'X-Atlassian-Token': 'nocheck',
                                                          'content-type': JIRA.SUPPRESS_CONTENT_TYPE_AUTODETECT})
        raise_on_error(r)

        attachment = Attachment(self._options, self._session, json.loads(r.text)[0])
        return attachment

0 votes
Jean-Martin Archer Jul 19, 2013

For people wondering, the library now support definition of the filename.

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Posted in Statuspage

⭐️Would you recommend Statuspage? Leave a (honest!) review. ⭐️

Hi Community friends, We're working on sourcing more reviews on Capterra – a popular software review site –  to help teams like yours make more informed decisions when choosing an inc...

82 views 3 5
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you