如何使用curl将文件上传到Atlassian Confluence页面

9 投票
4 回答
10892 浏览
提问于 2025-04-17 23:14

我正在尝试将一个 .xls 文件上传到 Confluence 维基页面,按照远程 API 文档中的指导进行操作: https://developer.atlassian.com/confdev/confluence-server-rest-api/confluence-rest-api-examples#ConfluenceRESTAPIExamples-Uploadanattachment

curl -v -S -u admin:admin -X POST -H "X-Atlassian-Token: nocheck" -F "file=@myfile.txt" -F "comment=this is my file" "http://localhost:8080/confluence/rest/api/content/3604482/child/attachment" | python -mjson.tool

这是我正在做的事情:

curl -v -S -u username:password -X POST -H "X-Atlassian-Token: nocheck" -F "file=@/path/to/local/excelsheet.xls" https://<Confluence server>/display/page

我省略了 Python -mjson.tool,因为它显示“无法解码 JSON 对象”,这对我来说没有意义,因为我并没有发送 JSON。

不过,上面的 curl 命令对我来说不起作用。我在控制台上看到的是目标页面的 HTML,而文件并没有上传。我尝试了多种方式修改 curl 命令,但都没有成功。

另外,我尝试上传的页面的 URL 中没有文档中建议的 contentID。这个目标 URL 是一个可以接受附件并显示已上传文件列表的页面。

有人能告诉我哪里出错了吗?我对 Curl 的经验不多。

4 个回答

0

API的POST语法需要从"https://ConfluenceServer/display/page"更正为正确的res/api/content语法,比如说"https://companyName.atlassian.net/display/rest/api/content/pageIDxxxxx"。

 curl -v -S  -X POST -H "X-Atlassian-Token: no-check" -F "file_0=@<file name>" -F "comment_0=<upload comment>" https://<companyName>.atlassian.net/display/rest/api/content/<pageID15398762>/child/attachment

你可以在显示页面的URL中找到pageID。例如:
https://companyName.atlassian.net/display/spaces/DEV/pages/pageID15398762/Page+Title+Name

想了解更多API语法的细节,可以参考这份文档:
https://docs.atlassian.com/atlassian-confluence/REST/6.6.0/#content-createContent

1

我觉得Confluence的REST API不支持文件上传。请你试试下面的方法。

curl -v -S -X POST -H "X-Atlassian-Token: nocheck" -F "file_0=@<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"

把所有的<...>占位符替换成你自己的值。

4

David Vonka 的回答是正确的,除了标题 "X-Atlassian-Token" 的值。这个值必须是 "no-check"(而不是 "nocheck")

所以修正后的命令是:

curl -v -S -X POST -H "X-Atlassian-Token: no-check" -F "file_0=@<file name>" -F "comment_0=<upload comment>" "http://<server>:<port>/<context>/pages/doattachfile.action?pageId=<page id>&os_username=<username>&os_password=<password>"

注意: 把所有的 <...> 占位符替换成你的实际值

3

你需要使用REST API,网址是:.../confluence/rest/api/content/$PAGE_ID/child/attachment,而你现在使用的是查看页面的链接。

撰写回答