从python脚本将文件上载到我的dropbox
我想自动将python脚本中的文件上传到dropbox帐户。我找不到任何方法只使用一个用户/通行证就可以做到这一点。我在Dropbox SDK中看到的一切都与具有用户交互的应用程序相关。我只想做这样的事情:从python脚本将文件上载到我的dropbox,python,upload,dropbox,Python,Upload,Dropbox,我想自动将python脚本中的文件上传到dropbox帐户。我找不到任何方法只使用一个用户/通行证就可以做到这一点。我在Dropbox SDK中看到的一切都与具有用户交互的应用程序相关。我只想做这样的事情: /?user=me&pass=blah对Dropbox API调用进行身份验证的唯一方法是使用OAuth,这涉及到用户授予应用程序权限。我们不允许第三方应用处理用户凭据(用户名和密码) 如果这只是针对您的帐户,请注意,您可以很容易地为您自己的帐户获取OAuth令牌并使用它。看 如果这是针对其
/?user=me&pass=blah对Dropbox API调用进行身份验证的唯一方法是使用OAuth,这涉及到用户授予应用程序权限。我们不允许第三方应用处理用户凭据(用户名和密码) 如果这只是针对您的帐户,请注意,您可以很容易地为您自己的帐户获取OAuth令牌并使用它。看
如果这是针对其他用户的,他们需要通过浏览器对您的应用程序进行一次授权,以便您获得OAuth令牌。但是,一旦您拥有令牌,您就可以继续使用它,因此每个用户只需执行一次。重要提示:由于dropbox现在使用v2 API,因此此答案已被弃用。
有关当前API版本解决方案,请参阅的答案 感谢@smarx提供上述答案!我只是想为其他任何试图这样做的人澄清一下
pip install dropbox
:
导入dropbox
client=dropbox.client.DropboxClient()
打印“链接帐户:”,客户端。帐户信息()
f=打开('working-draft.txt','rb')
response=client.put_文件('/magnum opus.txt',f)
打印“上传:”,回复
文件夹\u metadata=client.metadata('/'))
打印“元数据:”,文件夹\u元数据
f、 metadata=client.get_文件和_元数据('/magnum opus.txt'))
out=open('magnum-opus.txt','wb')
out.write(f.read())
结束
打印元数据
如果我遗漏了什么,很抱歉,但是你不能下载操作系统的dropbox应用程序,然后将文件(在windows中)保存到:
C:\Users\\Dropbox\
我刚刚添加了一个python程序来保存一个文本文件,选中了我的dropbox,它保存得很好。下面是在windows中使用python在dropbox上上传实况视频的代码。 希望这对你有帮助
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
# 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
下面是在windows中使用python在dropbox帐户上上载现有视频的代码 希望这对你有帮助
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
# 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
现在上传图像时,将使用相同的代码
只写下您要上传的图像文件名,例如:image.jpg,而不是视频名称。另外,请更改video1.avi的名称,并为您上传的图像写入名称,其中您的图像将显示在dropbox for ex:image1.jpg中。的答案基于dropbox,dropbox现已被弃用,并将于2017年6月28日关闭。(有关更多信息,请参阅。)
于2015年11月推出,更简单、更一致、更全面
以下是应用程序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()
源代码托管在GitHub上。下面是我使用API v2(和Python 3)的方法。我想上传一个文件并为它创建一个共享链接,我可以通过电子邮件发送给用户。这是基于斯巴康星的例子。注意:我认为当前的API文档有一个小错误,sparkandshine已经纠正了这个错误
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)
我能得到一个反对票的评论吗?所以我可以改进这个问题吗?官方SDK中包含了一些示例:哈哈。是的,你是。最初的海报试图通过编程实现这一点。准确地说,这是一个有效的解决方案。你可以“以编程方式”(我喜欢这个新词)将文件保存在相应的目录中,然后Dropbox应用程序将与你的帐户同步,而无需进一步的用户交互。问题是大多数人将应用程序部署到服务器,在这种情况下,则是另一台机器。似乎只需将文件复制到计算机上的“…Dropbox”文件夹中,并让Dropbox桌面应用程序处理上载,即可处理上载。然而,在某些情况下我们无法做到这一点:1)Dropbox同步可能非常慢,根据我的经验,使用API上传文件要快得多。2)用户没有桌面客户端安装他们的PC。考虑有3TB帐户的人,他们想要用来存档3TB价值的文件。他们的电脑必须有足够的额外存储空间(忽略选择性同步等技巧)。检查您的dropbox帐户是否正确上载。检查您的dropbox帐户是否正确上载。我唯一能让它工作的方法是将其更改为:dbx.files\u upload(f.read(),file\u to)@SteveLockwood,它在一年前进行了测试,结果成功了。无论如何,我按照你的建议更新了我的答案。我想知道这是否是python 2/3的区别——我的示例已经用python进行了测试3@SteveLockwood,我用python2测试了它。正如下面SparkAndShine所强调的,这是针对Dropbox API v1的,现在已弃用。如何将文件从S3 URL上载到Dropbox?应该注意,支持v2的较新Dropbox库需要太多不同的库,如果您在Arduino这样的最小linux环境中,它们会耗尽设备上的所有内存并崩溃:(就这么简单。工作起来很有魅力。这个答案应该更高。
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)