Utilize Confluence Data Center REST API in a Bash script to copy pages along with the Attachments.

Still need help?

The Atlassian Community is here for you.

Ask the community

Platform Notice: Data Center - This article applies to Atlassian products on the Data Center platform.

Note that this knowledge base article was created for the Data Center version of the product. Data Center knowledge base articles for non-Data Center-specific features may also work for Server versions of the product, however they have not been tested. Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.

*Except Fisheye and Crucible

Summary

  • Currently, Confluence does not provide a native method or API to automate the copying of a page along with its content and attachments.
  • A feature request is currently open to address this limitation CONFSERVER-60397.
  • In the meantime, to achieve this functionality, one can utilize the Confluence REST API to "get content" and then "create new content", subsequently downloading and uploading attachments to the newly created page. All of these tasks can be automated using a BASH script.

Solution

The solution is broken down into individual steps that we will then join together in a single shell script.

Define environment variables

These variables will be used along with the subsequent commands.

Some notes about these variables:

  • Make sure the user performing these changes has the appropriate Space Permissions and, if applicable, Page Restrictions. Note that Confluence REST API follows the same permissions/restrictions when the user is performing similar operations from the UI (browser).
  • The attachment type should be either image or file. This is used to determine the macro to preview the attachment that will be added to the page.
  • The ID of the target page should be known. See How to get Confluence page ID for how to get this value.
#!/bin/bash

# Confluence credentials and base URL
USERNAME="your_username"
PASSWORD="your_username"
BASE_URL="<Confluence_Base_URL>"
SPACE_KEY="<SpaceKey>"
PAGE_ID="<PageID>"

# Fetch page content
page_response=$(curl -s -u "$USERNAME:$PASSWORD" "$BASE_URL/rest/api/content/$PAGE_ID?expand=body.view,metadata.labels")
page_title=$(jq -r '.title' <<< "$page_response")
page_body=$(jq -r '.body.view.value' <<< "$page_response")
echo "Fecth Page is done"

# Create new page
new_page_data=$(jq -n --arg title "Copy of $page_title" --arg space "$SPACE_KEY" --arg body "$page_body" '{"type": "page", "title": $title, "space": {"key": $space}, "body": {"storage": {"value": $body, "representation": "storage"}}}')
new_page_response=$(curl -s -u "$USERNAME:$PASSWORD" -X POST -H "Content-Type: application/json" -d "$new_page_data" "$BASE_URL/rest/api/content")
new_page_id=$(jq -r '.id' <<< "$new_page_response")
echo "Page creation is done"

# Fetch attachments
attachments_response=$(curl -s -u "$USERNAME:$PASSWORD" "$BASE_URL/rest/api/content/$PAGE_ID/child/attachment")
attachments=$(jq -r '.results[] | @base64' <<< "$attachments_response")
echo "Fetch Attachment is done"

# Upload attachments
for attachment in $attachments; do
    attachment_name=$(echo "$attachment" | base64 -d | jq -r '.title')
    echo "attachment_name: $attachment_name"
    attachment_url=$(echo "$attachment" | base64 -d | jq -r '._links.download')
    echo "attachment_url: $BASE_URL/$attachment_url"
    curl -s -u "$USERNAME:$PASSWORD" -O "$BASE_URL$attachment_url"
    echo $?
    sleep 10
    curl -s -u "$USERNAME:$PASSWORD" -X POST -H "X-Atlassian-Token: nocheck" -F "file=@$attachment_name" -F "comment=File attached via REST API" "$BASE_URL/rest/api/content/$new_page_id/child/attachment"
    echo "Upload attachment is done"
done 



In case of issues or concerns to follow this procedure, please reach out to the Atlassian Support team and share the information that's been gathered so far along with a fresh Support zip file.

Last modified on Aug 14, 2024

Was this helpful?

Yes
No
Provide feedback about this article
Powered by Confluence and Scroll Viewport.