upload attach with same name on a page with new version with rest-api

kohopir975
Contributor
July 3, 2023

Hi

here is bashscript that upload file to confluence, first time it upload correctly second time return error because of file with same name exist. is there any way like we upload file with same name through the web UI and it will add new version of file, do the same thing with curl command?

 

Here is my bashscript:

curl -u $USER_NAME:$API_TOKEN \
 -X POST \
 -H "X-Atlassian-Token: nocheck" -F "file=@${ATTACHMENT_FILE_NAME}" -F "comment=File attached via REST API" \
 ${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment


here is the error:

{"statusCode":400,"data":{"authorized":false,"valid":true,"allowedInReadOnlyMode":true,"errors":[],"successful":false},"message":"Cannot add a new attachment with same file name as an existing attachment: result.csv","reason":"Bad Request"}

 

Any idea?

Thanks

 

2 answers

1 accepted

0 votes
Answer accepted
Markus Fredén
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.
July 3, 2023

Hi @kohopir975 ,

To upload a new version of an attachment with the same name in Confluence using the REST API, you can follow these steps:

1. Retrieve the existing attachment ID:
Before uploading a new version of the attachment, you need to know the ID of the existing attachment. You can use the Confluence REST API to retrieve this information. Here's an example command:


curl -u $USER_NAME:$API_TOKEN \
-X GET \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment?filename=${ATTACHMENT_FILE_NAME}

Replace `$USER_NAME`, `$API_TOKEN`, `${CONFLUENCE_BASE_URL}`, `${PAGE_ID}`, and `${ATTACHMENT_FILE_NAME}` with the appropriate values.

This command will return the JSON response containing the details of the existing attachment, including its ID.

2. Upload the new version of the attachment:
Once you have the existing attachment ID, you can upload the new version using the Confluence REST API. Here's an example command:


curl -u $USER_NAME:$API_TOKEN \
-X POST \
-H "X-Atlassian-Token: nocheck" \
-F "file=@${NEW_ATTACHMENT_FILE_NAME}" \
-F "minorEdit=true" \
${CONFLUENCE_BASE_URL}/rest/api/content/${PAGE_ID}/child/attachment/${EXISTING_ATTACHMENT_ID}/data

Replace `$USER_NAME`, `$API_TOKEN`, `${CONFLUENCE_BASE_URL}`, `${PAGE_ID}`, `${NEW_ATTACHMENT_FILE_NAME}`, and `${EXISTING_ATTACHMENT_ID}` with the appropriate values.

This command uploads the new file using the same name and specifies the `minorEdit` parameter as `true` to indicate that it's a minor edit.

By following these steps, you can upload a new version of an attachment with the same name in Confluence using the REST API.

/Markus

kohopir975
Contributor
July 3, 2023

@Markus Fredén Thanks work like a charm ;)

Like Markus Fredén likes this
0 votes
Stefan Draber
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.
September 24, 2024

Hi @kohopir975 

since I was just working on the same, I'd like to share my findings:

In your original code you used the method POST, which is exclusively for creating new attachments. If there already is an attachment with the same filename you're trying to upload, then it throws the reported error.

When you use PUT instead, then it checks if the file already exists and either creates a new or updates the version of an existing file with that filename.

API documentation for the PUT method: https://developer.atlassian.com/cloud/confluence/rest/v1/api-group-content---attachments/#api-wiki-rest-api-content-id-child-attachment-put

Compared with the other solution you don't have to identify the attachment-id first, so you can use it flexibly, no matter whether the page already contains this attachment or not.

Hope it still helps 🙂

Oliver
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 8, 2024

Hi @Stefan Draber 

When I use PUT, I get 405 Method not allowed back. POST to create the first attachment works without problems. The workaround from @Markus Fredén also works.

Stefan Draber
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.
October 9, 2024

Hi @Oliver 

I'm sorry that it doesn't work for you, for me it does. It's hard to troubleshoot without knowing your code.

But anyway, if the workaround is suitable for you, then it's alright I guess :)

Suggest an answer

Log in or Sign up to answer