【问题标题】:How to download a Confluence page attachment with Python?如何使用 Python 下载 Confluence 页面附件?
【发布时间】:2020-02-03 11:39:49
【问题描述】:

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

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

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

fname = wget.download(base_server_name + attachment['_links']['download'])

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

“结果”键:

{'id': '56427526', 'type': 'attachment', 'status': 'current', 'title': 'main.c', 'metadata': {'mediaType': 'application/八位字节流','标签':{'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', 'history ':'/rest/api/content/56427526/history','祖先':'','body':'','版本':'','后代':'/rest/api/content/56427526/后代', '空格': '/res t/api/space/~Tim'}}

【问题讨论】:

    标签: python confluence confluence-rest-api atlassian-python-api


    【解决方案1】:

    虽然我没有找到直接使用atlassian-python-api 模块下载文件的方法,但我设法使用requests 模块完成了下载,感谢this answer。这是用于下载页面中所有可见附件的代码:

    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)
    

    【讨论】:

      猜你喜欢
      • 2017-03-16
      • 2019-02-09
      • 2011-11-08
      • 2022-10-13
      • 2017-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多