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
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.
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.
Make sure you install the dropbox module first of course, pip install dropbox.
Create an app under your own dropbox account in the "App Console". (https://www.dropbox.com/developers/apps)
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".)
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
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)
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)
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.
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.
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
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))
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:\python27\output1.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
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:\python27\examples\video.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.
Related
We are planning to use Dropbox to get some csv files transfered from client location. This csv files need to be processed by a data engineering pipeline. We are using python to process this. As a first step , we need to download the file from dropbox to the local file folder. I have created an app using Dropbox App Console as Scoped App first. In the Python program we need to get an API Access token. And from the scopped App , I was not able to generate the Access token as I was getting error stating that " You need to be a Team administrator to generate the token". This was misleading as this was a single account i created for testing it out and no teams are present. I tried with another method which is using the user id and secret to prompt for an access token
here is the code :
class DropboxFolderCreation:
"""
This class is responsible for creating empty directories in dropbox account.
"""
def __init__(self):
# define your dropbox app key below
self.app_key = 'xxxxxxxxxxx'
# define your dropbox app secret key below
self.app_secret = 'xxxxxxxxxxxxx'
# define your CSV file path below
self.csv_path = 'example.csv'
def login_dropbox(self):
"""
Authorise Dropbox using OAuth 2.0
Follow instructions and authorise your Dropbox account.
"""
APP_KEY = self.app_key
APP_SECRET = self.app_secret
auth_flow = dropbox.DropboxOAuth2FlowNoRedirect(APP_KEY, APP_SECRET)
authorize_url = auth_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.")
auth_code = input("Enter the authorization code here: ").strip()
try:
oauth_result = auth_flow.finish(auth_code)
except Exception as e:
print("Error: %s" % (e,))
return oauth_result
def read_csv(self,dbx):
"""
read .csv file and extract directory names
"""
"""wb = open_workbook(self.csv_path).sheet_by_index(0)
directory_list = []
# if csv file contains data from row 2 then set start_rowx = 1
# else set it to 0 as below
# first argument is set to 0 since we want to read column 1 of csv
csv_data = wb.col_values(0, start_rowx = 0)"""
#dbx = dropbox.Dropbox(<access_token>)
metadata, f = dbx.files_download(self.csv_path)
print(metadata)
csv_reader = csv.reader(f.content.decode().splitlines(), delimiter=',')
with open(metadata) as file:
line_count = 0
for row in csv_reader:
if line_count == 0:
print(f'Column names are {", ".join(row)}')
line_count += 1
else:
print(row)
line_count += 1
print(f'Processed {line_count} lines.')
return csv_data
def create_dirs_on_dropbox(self):
"""
Create empty directories in Dropbox account using API v2
"""
token = self.login_dropbox()
dbx = dropbox.Dropbox(token.access_token)
dirs = self.read_csv(dbx)
csv_data = self.read_csv(dbx)
if csv_data:
#doing something here
print("Successfully download file from your dropbox account ")
else:
print("could not read data from csv file")
And when executing the below :
dbx_instance = DropboxFolderCreation()
dbx_instance.create_dirs_on_dropbox()
1. Go to: https://www.dropbox.com/oauth2/authorize?response_type=code&client_id=a9hg3hhu85613yv
2. Click "Allow" (you might have to log in first).
3. Copy the authorization code.
Enter the authorization code here: dTuX5n5_iV0AAAAAAAAAKX5Rrskr-ZCroPMjuSK2qMo
Connection to Dropbox is successful , but getting error while trying to access the file
error as :
ValidationError: 'ListPriceChanges.csv' did not match pattern '(/(.|[\r\n])*|id:.*)|(rev:[0-9a-f]{9,})|(ns:[0-9]+(/.*)?)'
-I suspected this error is coming because I am not able to read the folder list which I verified using this
response = dbx.files_list_folder(path="")
print(response)
which returns an empty list.
So My problem is how to generate the access token for the scoped App . Do we have any simple way to connect and download the files ?
We have similar issues . When you use scope application, do not select any scope related to teams ,
from the App console, select your application, Click on the scoped App, and deselect everything under the team scope
you can come back and generate the access token now.
Once you have access token for your scoped App, then it is pretty straight forward
import dropbox
dbx = dropbox.Dropbox("access token")
with open("example.csv", "wb") as f:
metadata, res = dbx.files_download(path="/example.csv")
f.write(res.content)
The above code will download the example.csv from the root folder . If you have the file under any folder say test then in the path you need to specify /test/example.csv
As the title says, I have access to a shared folder where some files are uploaded. I just want to donwload an specific file, called "db.dta". So, I have this script:
def download_file(url, filename):
url = url
file_name = filename
with open(file_name, "wb") as f:
print("Downloading %s" % file_name)
response = requests.get(url, stream=True)
total_length = response.headers.get('content-length')
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)) )
sys.stdout.flush()
print(" ")
print('Descarga existosa.')
It actually download shares links of files if I modify the dl=0 to 1, like this:
https://www.dropbox.com/s/ajklhfalsdfl/db_test.dta?dl=1
The thing is, I dont have the share link of this particular file in this shared folder, so if I use the url of the file preview, I get an error of denied access (even if I change dl=0 to 1).
https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ?dl=1&preview=db.dta
Error given:
dropbox.exceptions.ApiError: ApiError('22eaf5ee05614d2d9726b948f59a9ec7', GetSharedLinkFileError('shared_link_access_denied', None))
Is there a way to download this file?
If you have the shared link to the parent folder and not the specific file you want, you can use the /2/sharing/get_shared_link_file endpoint to download just the specific file.
In the Dropbox API v2 Python SDK, that's the sharing_get_shared_link_file method (or sharing_get_shared_link_file_to_file). Based on the error output you shared, it looks like you are already using that (though not in the particular code snippet you posted).
Using that would look like this:
import dropbox
dbx = dropbox.Dropbox(ACCESS_TOKEN)
folder_shared_link = "https://www.dropbox.com/sh/a630ksuyrtw33yo/LKExc-MKDKIIWJMLKFJ"
file_relative_path = "/db.dat"
res = dbx.sharing_get_shared_link_file(url=folder_shared_link, path=file_relative_path)
print("Metadata: %s" % res[0])
print("File data: %s bytes" % len(res[1].content))
(You mentioned both "db.dat" and "db.dta" in your question. Make sure you use whichever is actually correct.)
Additionally, note if you using a Dropbox API app registered with the "app folder" access type: there's currently a bug that can cause this shared_link_access_denied error when using this method with an access token for an app folder app.
How to download files from a Box location programmatically?
I have a shared box location URL(Not the exact path of the box location).
I want to download all the files under the location.
I checked below sdk to connect to box but unable to find methods/library to download files from a shared link.
https://github.com/box/box-python-sdk
from boxsdk import Client
from boxsdk import OAuth2
oauth = OAuth2(
client_id='XXX',
client_secret='XXX',
store_tokens='XXX',
)
data = client.make_request(
'GET',
'<Shared BOX URL>',
)
Please help
Get metadata of shared Box link:
shared_folder = client.get_shared_item("https://app.box.com/s/0123456789abcdef0123456789abcdef")
Loop through each item inside the folder and download each file using boxsdk.object.file.File.content or boxsdk.object.file.File.download_to:
for item in shared_folder.get_items(limit=1000):
if item.type == 'file':
# Get file contents into memory
file_contents = client.file(file_id=item.id).content()
# Or download to file
client.file(file_id=item.id).download_to(item.name)
You can use the method that gives you the direct URL:
download_url = client.file(file_id='SOME_FILE_ID').get_shared_link_download_url()
And then you can use urlib to download it to your local computer:
import urllib
urllib.urlretrieve (download_url , your_local_file_name)
Could it solve your problem?
Pre-requisite:
oauth = OAuth2(
client_id = 'strBoxClientID',
client_secret = 'strBoxClientSecret',
access_token = access_token,
)
client = Client(oauth)
Initial attempt (failed, it produces an empty file):
with open(Box_File.name, 'wb') as open_file:
client.file(Box_File.id).download_to(open_file)
open_file.close()
Final solution:
output_file = open('strFilePath' + str(Box_File.name), 'wb')
Box_File.download_to(output_file)
Environment: Windows 7, Python Tools for Visual Studio, Python 2.7, Python Package dropbox(6.9.0), Access Token from my Dropbox account
The following code is run:
import dropbox
access_token = '<token value here>'
dbx = dropbox.Dropbox(access_token)
with open("C:\Test.txt", "w") as f:
metadata, res = dbx.files_download(path="/Test.txt")
f.write(res.content)
It errors on the last line with the following:
"No disassembly available"
I don't understand the error not being a Python developer.. the file is created on the local machine but nothing is downloaded into it from the dropbox file..
Any help would be greatly appreciated.. Thanks
python code for dropbox download with business API:
def dropbox_file_download(access_token,dropbox_file_path,local_folder_name):
try:
dropbox_file_name = dropbox_file_path.split('/')[-1]
dropbox_file_path = '/'.join(dropbox_file_path.split('/')[:-1])
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_user_name:
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)
# list all the files from the folder
result = dbx.files_list_folder(dropbox_file_path, recursive=False)
# download given file from dropbox
for entry in result.entries:
if isinstance(entry, dropbox.files.FileMetadata):
if entry.name == dropbox_file_name:
dbx.files_download_to_file(local_folder_name+entry.name, entry.path_lower)
return True
return False
except Exception as e:
print(e)
return False
import dropbox
access_token = '**********************'
dbx = dropbox.Dropbox(access_token)
f = open("ABC.txt","w")
metadata,res = dbx.files_download("abc.txt") //dropbox file path
f.write(res.content)
I'm looking for a solution on how to upload a picture from an external url like http://example.com/image.jpg to google cloud storage using appengine python,
I am now using
blobstore.create_upload_url('/uploadSuccess', gs_bucket_name=bucketPath)
for users that want to upload a picture from their computer, calling
images.get_serving_url(gsk,size=180,crop=True)
on uploadSuccess and storing that as their profile image. I'm trying to allow users to use their facebook or google profile picture after they login with oauth2. I have access to their profile picture link, and I would just like to copy it for consistency. Pease help :)
To upload an external image you have to get it and save it.
To get the image you van use this code:
from google.appengine.api import urlfetch
file_name = 'image.jpg'
url = 'http://example.com/%s' % file_name
result = urlfetch.fetch(url)
if result.status_code == 200:
doSomethingWithResult(result.content)
To save the image you can use the app engine GCS client code shown here
import cloudstorage as gcs
import mimetypes
doSomethingWithResult(content):
gcs_file_name = '/%s/%s' % ('bucket_name', file_name)
content_type = mimetypes.guess_type(file_name)[0]
with gcs.open(gcs_file_name, 'w', content_type=content_type,
options={b'x-goog-acl': b'public-read'}) as f:
f.write(content)
return images.get_serving_url(blobstore.create_gs_key('/gs' + gcs_file_name))
Here is my new solution (2019) using the google-cloud-storage library and upload_from_string() function only (see here):
from google.cloud import storage
import urllib.request
BUCKET_NAME = "[project_name].appspot.com" # change project_name placeholder to your preferences
BUCKET_FILE_PATH = "path/to/your/images" # change this path
def upload_image_from_url_to_google_storage(img_url, img_name):
"""
Uploads an image from a URL source to google storage.
- img_url: string URL of the image, e.g. https://picsum.photos/200/200
- img_name: string name of the image file to be stored
"""
storage_client = storage.Client()
bucket = storage_client.get_bucket(BUCKET_NAME)
blob = bucket.blob(BUCKET_FILE_PATH + "/" + img_name + ".jpg")
# try to read the image URL
try:
with urllib.request.urlopen(img_url) as response:
# check if URL contains an image
info = response.info()
if(info.get_content_type().startswith("image")):
blob.upload_from_string(response.read(), content_type=info.get_content_type())
print("Uploaded image from: " + img_url)
else:
print("Could not upload image. No image data type in URL")
except Exception:
print('Could not upload image. Generic exception: ' + traceback.format_exc())
If you're looking for an updated way of doing this relying on storages package, I wrote those 2 functions:
import requests
from storages.backends.gcloud import GoogleCloudStorage
def download_file(file_url, file_name):
response = requests.get(file_url)
if response.status_code == 200:
upload_to_gc(response.content, file_name)
def upload_to_gc(content, file_name):
gc_file_name = "{}/{}".format("some_container_name_here", file_name)
with GoogleCloudStorage().open(name=gc_file_name, mode='w') as f:
f.write(content)
Then normally call download_file() and pass url and prefered_file_name from anywhere within your system.
The class GoogleCloudStorage came from django-storages package.
pip install django-storages
Django Storages