2

使用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'])

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

“结果”键:

{'id': '56427526', 'type': 'attachment', 'status': 'current', 'title': 'main.c', 'metadata': {'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', 'restrictions' :'/rest/api/content/56427526/restriction/byOperation','历史':'/rest/api/content/56427526/history','祖先':'','body':'','版本' :'','后代':'/rest/api/content/56427526/descendant','空间':'/rest/api/space/~Tim'}}

4

1 回答 1

7

虽然我没有找到直接使用模块下载文件的方法,但由于这个答案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)
于 2020-02-03T12:52:03.647 回答