Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

file 0KB download using python from confluence page

Ehab July 4, 2022

Hello,

I'm trying to download all attachments from confluence page using the following python code. the downloaded files are 0 kb

I changed the authorization to be using username and password

the result is the same

what could be wrong? how can I fix it?


import os, os.path
from atlassian import Confluence
import requests
import urllib3

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

path = r'<path for downloaded files>'
page = '<Space URL>'

confluence = Confluence(url= page, verify_ssl = False, token = "<Peronal Token>")

attachments_container = confluence.get_attachments_from_content(page_id=<page id>, 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, headers={"Authorization": "<Peronal Token>"} , verify = False)
if r.status_code == 200:
print('Downloading ' + fname + ' File!')
file = open(os.path.join(path, fname), 'wb')
file.write(r.content)
else:
print(r.status_code)
print('not downloaded')

 


files in the confluence 

confluence page attachments.png

downloaded files

downloaded files.png


 

MyPassword = getpass.getpass()
confluence = Confluence(url= page, verify_ssl = False, username='<username>', password=MyPassword)

. . . 

r = requests.get(download_link, auth=(confluence.username, confluence.password), verify = False)

1 answer

0 votes
Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 4, 2022

I don't think the variable "download link" contains what you think it does.

You should be using a structured REST call to get the attachment, as documented at https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issue-attachments/#api-rest-api-3-attachment-content-id-get 

I can't see how your code would be generating that REST call.

Ehab July 5, 2022

Thanks, Nic,

the code generates a download URL for the attachment. This is the result of printing the download link

URLs.png

the file is downloaded when I put the URL in the browser. 

regarding the "Get attachment content", I cannot get the ID of the attachment

Nic Brough -Adaptavist-
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 5, 2022

Ok, so you're using the wrong url - your script is not a browser, so it's not got all the functions you need to use a direct download.

You need to swap to using the REST api.

Ehab July 5, 2022

Thank you

how can I get the ID of the attachment in a confluence page

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events