Utilize Confluence REST API in a Bash script to copy pages along with attachments.

お困りですか?

アトラシアン コミュニティをご利用ください。

コミュニティに質問

プラットフォームについて: Data Center のみ - この記事は、Data Center プラットフォームのアトラシアン製品にのみ適用されます。

この KB は Data Center バージョンの製品用に作成されています。Data Center 固有ではない機能の Data Center KB は、製品のサーバー バージョンでも動作する可能性はありますが、テストは行われていません。サーバー*製品のサポートは 2024 年 2 月 15 日に終了しました。サーバー製品を利用している場合は、アトラシアンのサーバー製品のサポート終了のお知らせページにて移行オプションをご確認ください。

*Fisheye および Crucible は除く

要約

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.

ソリューション

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 "Fecth 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 




最終更新日 2024 年 4 月 16 日

この内容はお役に立ちましたか?

はい
いいえ
この記事についてのフィードバックを送信する
Powered by Confluence and Scroll Viewport.