Skip to content

Instantly share code, notes, and snippets.

@ahurriyetoglu
Last active April 21, 2023 20:04
Show Gist options
  • Save ahurriyetoglu/2523c0cfe3489a28bf2eb45086738785 to your computer and use it in GitHub Desktop.
Save ahurriyetoglu/2523c0cfe3489a28bf2eb45086738785 to your computer and use it in GitHub Desktop.
You can use Dropbox SDK with its Python client to upload files or folders directly to your Dropbox account. This code handles files independent of their size.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'10.8.0'"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import os\n",
"from tqdm import tqdm\n",
"import dropbox # you need Dropbox SDK client for Python\n",
"dropbox.__version__"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# https://stackoverflow.com/questions/37397966/dropbox-api-v2-upload-large-files-using-python\n",
"# the following code will upload any file independent of its size.\n",
"def upload_large_file(drbx, file_p, target_p):\n",
"# print('the file_p is:', file_p)\n",
"# print('the target_p is:', target_p)\n",
" with open(file_p, \"rb\") as f:\n",
" file_size = os.path.getsize(file_p)\n",
" chunk_size = 4 * 1024 * 1024\n",
" if file_size <= chunk_size:\n",
" print(drbx.files_upload(f.read(), target_p))\n",
" else:\n",
" with tqdm(total=file_size, desc=\"Uploaded\") as pbar:\n",
" upload_session_start_result = drbx.files_upload_session_start(\n",
" f.read(chunk_size)\n",
" )\n",
" pbar.update(chunk_size)\n",
" cursor = dropbox.files.UploadSessionCursor(\n",
" session_id=upload_session_start_result.session_id,\n",
" offset=f.tell(),\n",
" )\n",
" commit = dropbox.files.CommitInfo(path=target_p)\n",
" while f.tell() < file_size:\n",
" if (file_size - f.tell()) <= chunk_size:\n",
" print(\n",
" drbx.files_upload_session_finish(\n",
" f.read(chunk_size), cursor, commit\n",
" )\n",
" )\n",
" else:\n",
" drbx.files_upload_session_append(\n",
" f.read(chunk_size),\n",
" cursor.session_id,\n",
" cursor.offset,\n",
" )\n",
" cursor.offset = f.tell()\n",
" pbar.update(chunk_size)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# 1- The app_key and app_secret should be created on your Dropbox count. you should first create an application.\n",
"# 2- The application name will appear as a folder name under the folder Dropbox/Apps/<application-name>\n",
"# 3- This code will open a window in the browser you are using (or the default one) and ask for authorization.\n",
"# You should confirm the request and copy-paste the authorization code.\n",
"app_key = 'enter-your-app-key'\n",
"app_secret = 'enter-your-app-secret'\n",
"\n",
"flow = dropbox.DropboxOAuth2FlowNoRedirect(app_key, app_secret)\n",
"\n",
"authorize_url = flow.start()\n",
"print('1. Go to: ' + authorize_url)\n",
"print('2. Click \"Allow\" (you might have to log in first)')\n",
"print('3. Copy the authorization code.')\n",
"code = input(\"Enter the authorization code here: \").strip()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"connection successful!\n"
]
}
],
"source": [
"try:\n",
" oauth_result = flow.finish(code)\n",
" print('connection successful!')\n",
"except Exception as e:\n",
" print('Error: %s' % (e,))\n",
" exit(1)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Successfully set up client!\n"
]
}
],
"source": [
"with dropbox.Dropbox(oauth2_access_token=oauth_result.access_token, timeout=900) as dbx:\n",
" dbx.users_get_current_account()\n",
" print(\"Successfully set up client!\")"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['http___www.eluniversal.com.mx_alejandro-hope_nacion_la-otra-catastrofe', 'http___www.eluniversal.com.mx_alfonso-zarate_nacion_la-rendicion', 'http___www.eluniversal.com.mx_alfredo-sanchez-castaneda_una-reforma-laboral-lamentable']\n"
]
}
],
"source": [
"# in case you already have some files there and you do not want to upload them again. This is particularly helpful\n",
"# if a folder contains thousands of documents.\n",
"uploaded_urls = []\n",
"with open('uploadedurls.txt') as f:\n",
" uploaded_urls = [fn.strip() for fn in f.readlines()]\n",
"\n",
"print(uploaded_urls[:3])\n",
"uploaded_urls = set(uploaded_urls)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# This file or folder will be uploaded to Dropbox/Apps/<App Name>/file_path\n",
"file_path = '<file or folder path relative to this code file>'\n",
"\n",
"if os.path.isfile(file_path):\n",
" print('it is a single file. It will be uploaded.')\n",
" upload_large_file(dbx, file_path, file_path)\n",
"else:\n",
" print('it is a folder. All will be uploaded:')\n",
" for root, dirs, files in os.walk(file_path):\n",
" print('dirs is:', dirs)\n",
" for file in files:\n",
" if file.startswith('.') or file.startswith('~') or file.endswith('~'):\n",
" continue\n",
" if file in uploaded_urls:\n",
" print('.',end='')\n",
" continue\n",
"# print('the file is:',root+file)\n",
" \n",
" target_path = os.path.join(root, file)\n",
" if not target_path.startswith('/'):\n",
" target_path = '/'+target_path\n",
" \n",
"# print('the file will be uploaded to:', target_path)\n",
" upload_large_file(dbx, os.path.join(root, file), target_path)\n",
" \n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment