如何使用Python下载Confluence页面附件? [英] How to download a Confluence page attachment with Python?

查看:167
本文介绍了如何使用Python下载Confluence页面附件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

The page actions section of the API documentation mentions an API get_attachments_from_content, with which I can successfully obtain a list of all page attachments, with their metadata. There's an example at the end of this question of what I can obtain by printing one of the items in the results key.

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

What I already tried is to use the wget module to downloaad the attachment:

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

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

However, the downloaded file is not the one on the page, instead I have a large HTML file which looks like a light login page. Also, I'm not sure using wget is relevant here, I'd prefer a solution with the atlassian python API itself, as it's managing authentication by itself.

结果"键:

{'id':'56427526','type':'attachment','status':'current','title':'main.c','metadata':{'mediaType':'application/八位字节流','标签':{'结果':[],'开始':0,'极限':200,'大小':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':'

{'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': '/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', 'operations': '', 'children': '/rest/api/content/56427526/child', 'restrictions': '/rest/api/content/56427526/restriction/byOperation', 'history': '/rest/api/content/56427526/history', 'ancestors': '', 'body': '', 'version': '', 'descendants': '/rest/api/content/56427526/descendant', 'space': '/rest/api/space/~Tim'}}

推荐答案

虽然我找不到使用 atlassian-python-api 模块直接下载文件的方法,但我设法做到了感谢此答案.这是用于下载页面中可见的所有附件的代码:

While I didn't find a way to download the files directly with the atlassian-python-api module, I managed to do it with the requests module, thanks to this answer. Here's the code used to download all attachments visible in the page:

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)

这篇关于如何使用Python下载Confluence页面附件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆