如何使用 Python 下载 Confluence 页面附件?

Tim*_*Tim 3 python confluence confluence-rest-api atlassian-python-api

使用atlassian-python-api 1.15.1模块,python 3.6 如何下载附加到 Confluence 页面的文件?

API 文档的页面操作部分提到了一个 API ,通过get_attachments_from_content它我可以成功获取所有页面附件及其元数据的列表。在这个问题的末尾有一个例子,我可以通过打印密钥中的一项来获得什么results

我已经尝试过使用该wget模块下载附件:

fname = wget.download(base_server_name + attachment['_links']['download'])
Run Code Online (Sandbox Code Playgroud)

然而,下载的文件不是页面上的文件,而是一个很大的 HTML 文件,看起来像一个轻型登录页面。另外,我不确定 usingwget在这里是否相关,我更喜欢使用 atlassian python API 本身的解决方案,因为它自己管理身份验证。

“结果”键:

{'id':'56427526','类型':'附件','状态':'当前','标题':'main.c','元数据':{'mediaType':'application/octet-stream ', 'labels': {'results': [], 'start': 0, 'limit': 200, 'size': 0, '_links': {'self': ' https://foo.bar . com/confluence/rest/api/content/56427526/label '}}, '_expandable': {'currentuser': '', 'properties': '', 'frontend': '', 'editorHtml': ''} }, 'extensions': {'mediaType': 'application/octet-stream', 'fileSize': 363, 'comment': ''}, '_links': {'webui': '/pages/viewpage.action? pageId=14648850&preview=%2F14648850%2F56427526%2Fmain.c', '下载': '/download/attachments/14648850/main.c?version=1&modificationDate=1580726185883&api=v2', 'self': ' https://foo. bar.com/confluence/rest/api/content/56427526 '}, '_expandable': {'container': '/rest/api/content/14648850', '操作': '', 'children': '/rest /api/content/56427526/child', '限制': '/rest/api/content/56427526/restriction/byOperation', '历史': '/rest/api/content/56427526/history', '祖先': '', 'body': '', '版本': '', '后代': '/rest/api/content/56427526/descendant', '空间': '/rest/api/space/~Tim'} }

Tim*_*Tim 11

虽然我没有找到直接使用模块下载文件的方法,但由于这个答案atlassian-python-api,我设法使用模块来下载文件。以下是用于下载页面中可见的所有附件的代码:requests

from atlassian import Confluence
import requests

confluence = Confluence(
    url="https://my.server.com/Confluence",
    username='MyUsername',
    password="MyPassword")

attachments_container = confluence.get_attachments_from_content(page_id=12345678, start=0, limit=500)
attachments = attachments_container['results']
for attachment in attachments:
        fname = attachment['title']
        download_link = confluence.url + attachment['_links']['download']
        r = requests.get(download_link, auth=(confluence.username, confluence.password))
        if r.status_code == 200:
            with open(fname, "wb") as f:
                for bits in r.iter_content():
                    f.write(bits)
Run Code Online (Sandbox Code Playgroud)