Skip to content

Commit 2a8a77f

Browse files
authored
Download attachments from page (#1283)
* [Confluence] new method confluence_download_attachments_from_page * [Confluence] new method confluence_download_attachments_from_page * added example --------- Co-authored-by: gkowalc <gkowalc>
1 parent 6a085e3 commit 2a8a77f

File tree

4 files changed

+56
-0
lines changed

4 files changed

+56
-0
lines changed

atlassian/confluence.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,38 @@ def attach_file(
12961296
comment=comment,
12971297
)
12981298

1299+
def download_attachments_from_page(self, page_id, path=None):
1300+
"""
1301+
Downloads all attachments from a page
1302+
:param page_id:
1303+
:param path: path to directory where attachments will be saved. If None, current working directory will be used.
1304+
:return info message: number of saved attachments + path to directory where attachments were saved:
1305+
"""
1306+
if path is None:
1307+
path = os.getcwd()
1308+
try:
1309+
attachments = self.get_attachments_from_content(page_id=page_id)["results"]
1310+
if not attachments:
1311+
return "No attachments found"
1312+
for attachment in attachments:
1313+
file_name = attachment["title"]
1314+
if not file_name:
1315+
file_name = attachment["id"] # if the attachment has no title, use attachment_id as a filename
1316+
download_link = self.url + attachment["_links"]["download"]
1317+
r = self._session.get(f"{download_link}")
1318+
file_path = os.path.join(path, file_name)
1319+
with open(file_path, "wb") as f:
1320+
f.write(r.content)
1321+
except NotADirectoryError:
1322+
raise NotADirectoryError("Verify if directory path is correct and/or if directory exists")
1323+
except PermissionError:
1324+
raise PermissionError(
1325+
"Directory found, but there is a problem with saving file to this directory. Check directory permissions"
1326+
)
1327+
except Exception as e:
1328+
raise e
1329+
return {"attachments downloaded": len(attachments), " to path ": path}
1330+
12991331
def delete_attachment(self, page_id, filename, version=None):
13001332
"""
13011333
Remove completely a file if version is None or delete version

docs/confluence.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ Page actions
120120
# automatically version the new file and keep the old one
121121
confluence.attach_content(content, name=None, content_type=None, page_id=None, title=None, space=None, comment=None)
122122
123+
# Download attachments from a page to local system.
124+
confluence.download_attachments(page_id, download_path, path=None)
125+
123126
# Remove completely a file if version is None or delete version
124127
confluence.delete_attachment(page_id, filename, version=None)
125128
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from atlassian import Confluence
2+
import os
3+
4+
host = "<cloud_instance_url/wiki>"
5+
username = "<user_email>"
6+
password = "<API_TOKEN>"
7+
confluence = Confluence(
8+
url=host,
9+
username=username,
10+
password=password,
11+
)
12+
13+
# this is the directory where the attachments will be saved.
14+
# In this example we use current working directory where script is executed + subdirectory 'attachment_tests'
15+
16+
current_dir = os.getcwd()
17+
my_path = current_dir + "/attachment_tests"
18+
page = 393464 # make sure the page id exists and has attachments
19+
20+
confluence.download_attachments_from_page(page)
21+
# Directory 'attachment_tests' should include saved attachment. If directory deosn't exist or if there is permission issue function should raise an error.

test.file.py

Whitespace-only changes.

0 commit comments

Comments
 (0)