upload file to my dropbox from python script

Question:

I want to upload a file from my python script to my dropbox account automatically. I can’t find anyway to do this with just a user/pass. Everything I see in the Dropbox SDK is related to an app having user interaction. I just want to do something like this:

https://api-content.dropbox.com/1/files_put//?user=me&pass=blah

Asked By: Christina

||

Answers:

The only way to authenticate calls to the Dropbox API is to use OAuth, which involves the user giving permission to your app. We don’t allow third-party apps to handle user credentials (username and password).

If this is just for your account, note that you can easily get an OAuth token for your own account and just use that. See https://www.dropbox.com/developers/blog/94/generate-an-access-token-for-your-own-account.

If this is for other users, they’ll need to authorize your app once via the browser for you to get an OAuth token. Once you have the token, you can keep using it, though, so each user should only have to do this once.

Answered By: user94559

Important Note: this answer is deprecated since dropbox uses v2 API now.
See the answer of @SparkAndShine for current API version solution

Thanks to @smarx for the answer above! I just wanted to clarify for anyone else trying to do this.

  1. Make sure you install the dropbox module first of course, pip install dropbox.

  2. Create an app under your own dropbox account in the “App Console”. (https://www.dropbox.com/developers/apps)

  3. Just for the record I created my App with the following:

    a. App Type as “Dropbox API APP”.

    b. Type of data access as “Files & Datastores”

    c. Folder access as “My app needs access to files already on Dropbox”. (ie: Permission Type as “Full Dropbox”.)

  4. Then click the “generate access token” button and cut/paste into the python example below in place of <auth_token>:

import dropbox client = dropbox.client.DropboxClient(<auth_token>) print 'linked account: ', client.account_info() f = open('working-draft.txt', 'rb') response = client.put_file('/magnum-opus.txt', f) print 'uploaded: ', response folder_metadata = client.metadata('/') print 'metadata: ', folder_metadata f, metadata = client.get_file_and_metadata('/magnum-opus.txt') out = open('magnum-opus.txt', 'wb') out.write(f.read()) out.close() print metadata

Answered By: Christina

Sorry if im missing something but cant you just download the dropbox application for your OS and then save the file (in windows) in:

C:Users<UserName>Dropbox<FileName>

i just ceated a python program to save a text file, checked my dropbox and it saves them fine.

Answered By: Caleb Connolly

Here is the code for uploading livevideo on dropbox using python in windows.
Hope this will help you.

import numpy as np
import cv2
import dropbox
import os
from glob import iglob


access_token = 'paste your access token here'   #paste your access token in-between ''
client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()
PATH = ''

cap = cv2.VideoCapture(0)


# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('C:python27output1.avi',fourcc, 20.0, (640,480))

#here output1.avi is the filename in which your video which is captured from webcam is stored. and it resides in C:python27 as per the path is given.

while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
#frame = cv2.flip(frame,0) #if u want to flip your video

# write the (unflipped or flipped) frame
out.write(frame)

cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

for filename in iglob(os.path.join(PATH, 'C:/Python27/output1.avi')):
print filename
try:
f = open(filename, 'rb')
response = client.put_file('/livevideo1.avi', f)
print "uploaded:", response
f.close()
#os.remove(filename)
except Exception, e:
print 'Error %s' % e
Answered By: jasminavanasiwala

Here is the code for uploading existing video on your dropbox account using python in windows.

Hope this will help you.

# Include the Dropbox SDK
import dropbox

# Get your app key and secret from the Dropbox developer website
app_key = 'paste your app-key here'
app_secret = 'paste your app-secret here'

flow = dropbox.client.DropboxOAuth2FlowNoRedirect(app_key, app_secret)

# Have the user sign in and authorize this token
authorize_url = flow.start()
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()

# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)

client = dropbox.client.DropboxClient(access_token)
print 'linked account: ', client.account_info()

f = open('give full path of the video which u want to upload on your dropbox account(ex: C:python27examplesvideo.avi)', 'rb')
response = client.put_file('/video1.avi', f) #video1.avi is the name in which your video is shown on your dropbox account. You can give any name here.
print 'uploaded: ', response

folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata

f, metadata = client.get_file_and_metadata('/video1.avi')
out = open('video1.avi', 'wb')
out.write(f.read())
out.close()
print metadata

Now for uploading images, the same code will be used.

Only write your image file name which you want to upload for ex: image.jpg in place of video name . Also change the name of video1.avi and write name for image in which your uploaded image will be shown in your dropbox for ex:image1.jpg.

Answered By: jasminavanasiwala

The answer of @Christina is based on Dropbox APP v1, which is deprecated now and will be turned off on 6/28/2017. (Refer to here for more information.)

APP v2 is launched in November, 2015 which is simpler, more consistent, and more comprehensive.

Here is the source code with APP v2.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import dropbox

class TransferData:
    def __init__(self, access_token):
        self.access_token = access_token

    def upload_file(self, file_from, file_to):
        """upload a file to Dropbox using API v2
        """
        dbx = dropbox.Dropbox(self.access_token)

        with open(file_from, 'rb') as f:
            dbx.files_upload(f.read(), file_to)

def main():
    access_token = '******'
    transferData = TransferData(access_token)

    file_from = 'test.txt'
    file_to = '/test_dropbox/test.txt'  # The full path to upload the file to, including the file name

    # API v2
    transferData.upload_file(file_from, file_to)

if __name__ == '__main__':
    main()

The source code is hosted on GitHub, here.

Answered By: SparkAndShine

Here’s my approach using API v2 (and Python 3). I wanted to upload a file and create a share link for it, which I could email to users. It’s based on sparkandshine’s example. Note I think the current API documentation has a small error which sparkandshine has corrected.

import pathlib
import dropbox
import re

# the source file
folder = pathlib.Path(".")    # located in this folder
filename = "test.txt"         # file name
filepath = folder / filename  # path object, defining the file

# target location in Dropbox
target = "/Temp/"              # the target folder
targetfile = target + filename   # the target path and file name

# Create a dropbox object using an API v2 key
d = dropbox.Dropbox(your_api_access_token)

# open the file and upload it
with filepath.open("rb") as f:
   # upload gives you metadata about the file
   # we want to overwite any previous version of the file
   meta = d.files_upload(f.read(), targetfile, mode=dropbox.files.WriteMode("overwrite"))

# create a shared link
link = d.sharing_create_shared_link(targetfile)

# url which can be shared
url = link.url

# link which directly downloads by replacing ?dl=0 with ?dl=1
dl_url = re.sub(r"?dl=0", "?dl=1", url)
print (dl_url)
Answered By: Steve Lockwood
import dropbox
access_token = '************************'
file_from = 'index.jpeg'  //local file path
file_to = '/Siva/index.jpeg'      // dropbox path
def upload_file(file_from, file_to):
    dbx = dropbox.Dropbox(access_token)
    f = open(file_from, 'rb')
    dbx.files_upload(f.read(), file_to)
upload_file(file_from,file_to)
Answered By: user7384403

For Dropbox Business API below python code helps uploading files to dropbox.

def dropbox_file_upload(access_token,dropbox_file_path,local_file_name):
'''
The function upload file to dropbox.

    Parameters:
        access_token(str): Access token to authinticate dropbox
        dropbox_file_path(str): dropboth file path along with file name
        Eg: '/ab/Input/f_name.xlsx'
        local_file_name(str): local file name with path from where file needs to be uploaded
        Eg: 'f_name.xlsx' # if working directory
Returns:
    Boolean: 
        True on successful upload
        False on unsuccessful upload
'''
try:
    dbx = dropbox.DropboxTeam(access_token)
    # get the team member id for common user
    members = dbx.team_members_list()
    for i in range(0,len(members.members)):
        if members.members[i].profile.name.display_name == logged_in_user:
            member_id = members.members[i].profile.team_member_id
            break
    # connect to dropbox with member id
    dbx = dropbox.DropboxTeam(access_token).as_user(member_id)
    # upload local file to dropbox
    f = open(local_file_name, 'rb')
    dbx.files_upload(f.read(),dropbox_file_path)
    return True
except Exception as e:
    print(e)
    return False
Answered By: Vishal Telmani

If you need to upload a BIG file, you need to break up the file into chunks and upload the chunks one by one as follows. Inspired by this great medium artcle:

def upload_a_big_file(local_file_path: str, remote_file_path: str):
    # grab your authenticated client
    dbx = get_dropbox_client()
    file_size = os.path.getsize(local_file_path)
    # Upload 8 MB chunks at a time
    CHUNK_SIZE = 8 * 1024 * 1024

    with open(local_file_path, 'rb') as local_file:
        uploaded_size = 0
        upload_session_start_result = dbx.files_upload_session_start(local_file.read(CHUNK_SIZE))
        cursor = dropbox.files.UploadSessionCursor(
            session_id=upload_session_start_result.session_id,
            offset=local_file.tell()
        )
        commit = dropbox.files.CommitInfo(
            path=remote_file_path,
            mode=dropbox.files.WriteMode.overwrite
        )

        print("Starting Upload.")
        while local_file.tell() <= file_size: 
            if ((file_size - local_file.tell()) <= CHUNK_SIZE):
                # Last chunk remaining, so commit
                dbx.files_upload_session_finish(
                    local_file.read(CHUNK_SIZE),
                    cursor,
                    commit
                )
                print("Done uploading !")
                break
            else:
                dbx.files_upload_session_append_v2(
                    local_file.read(CHUNK_SIZE),
                    cursor
                )
                cursor.offset = local_file.tell()
                uploaded_size += CHUNK_SIZE
                uploaded_percent = 100*uploaded_size/file_size
                print('Uploaded {:.2f}%'.format(uploaded_percent))
Answered By: Harsh Verma
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.