Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example for using the "create_attachment" function? #129

Closed
TheMechanic97 opened this issue Oct 11, 2018 · 4 comments
Closed

Example for using the "create_attachment" function? #129

TheMechanic97 opened this issue Oct 11, 2018 · 4 comments
Assignees
Labels

Comments

@TheMechanic97
Copy link

I have tried using the path to a file as the upload_stream as well as opening the file to use the contents.

Using the path returns an ID and URL but they lead to a null (File does not exist) exception.

Using the contents of the file result in "'utf8' codec can't decode byte 0xff in position 0: invalid start byte".

@tedchamb
Copy link
Member

Similar issue to #29. When we fix #29, this should be fixed as well.

@tedchamb
Copy link
Member

Fix is in #163

@tedchamb
Copy link
Member

The fix has been released in 0.1.24

Example method to upload a file and link it as a work item attachment:

def upload_work_item_attachment(wit_client, work_item_id, file_name, callback=None):
    # upload file / create attachment
    with open(file_name, 'r+b') as file:
        # use mmap, so we don't load entire file in memory at the same time, and so we can start
        # streaming before we are done reading the file.
        mm = mmap.mmap(file.fileno(), 0)
        attachment = wit_client.create_attachment(mm, file_name=file_name, callback=callback)

    # Link Work Item to attachment
    patch_document = [
        JsonPatchOperation(
            op="add",
            path="/relations/-",
            value={
                "rel": "AttachedFile",
                "url": attachment.url
            }
        )
    ]
    wit_client.update_work_item(patch_document, work_item_id)

@jfthuong
Copy link

I confirm, upload works fine now!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants