forked from HackMidwest/hackmidwest2024
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdropbox_fetch.py
33 lines (24 loc) · 1.03 KB
/
dropbox_fetch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import dropbox, os
def download_image_from_dropbox():
ACCESS_TOKEN = os.getenv('DROPBOX_TOKEN')
if ACCESS_TOKEN == None:
raise ValueError("Dropbox token is not set! Use $DROPBOX_TOKEN")
dropbox_path = '/photo.jpg'
local_dir = 'static/assets/images'
# Create a Dropbox client instance
dbx = dropbox.Dropbox(ACCESS_TOKEN)
# Get the filename from the Dropbox path
filename = dropbox_path.split('/')[-1]
# Full local path where the image will be saved
local_file_path = f'{local_dir}/{filename}'
try:
# Download the file from Dropbox
metadata, res = dbx.files_download(dropbox_path)
# Write the file content to the local file
with open(local_file_path, 'wb') as f:
f.write(res.content)
print(f"Image downloaded successfully and saved to {local_file_path}")
return local_file_path
except dropbox.exceptions.ApiError as err:
print(f"Failed to download image from Dropbox: {err}")
return None