从 python 脚本上传文件到我的保管箱
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23894221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
upload file to my dropbox from python script
提问by Christina
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:
我想将文件从我的 python 脚本自动上传到我的 Dropbox 帐户。无论如何,我无法仅凭用户/通行证就可以做到这一点。我在 Dropbox SDK 中看到的一切都与具有用户交互的应用程序有关。我只想做这样的事情:
https://api-content.dropbox.com/1/files_put//?user=me&pass=blah
https://api-content.dropbox.com/1/files_put//?user=me&pass=blah
采纳答案by Christina
Important Note:this answer is deprecated since dropbox uses v2 API now.
See the answer of @SparkAndShinefor current API version solution
重要说明:此答案已弃用,因为 dropbox 现在使用 v2 API。
查看@SparkAndShine的答案以了解当前的 API 版本解决方案
Thanks to @smarx for the answer above! I just wanted to clarify for anyone else trying to do this.
感谢@smarx 提供上述答案!我只是想为其他任何试图这样做的人澄清一下。
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>
:
当然,请确保首先安装 dropbox 模块
pip install dropbox
。在“应用程序控制台”中,在您自己的保管箱帐户下创建一个应用程序。( https://www.dropbox.com/developers/apps)
只是为了记录,我使用以下内容创建了我的应用程序:
一种。应用类型为“Dropbox API APP”。
湾 数据访问类型为“文件和数据存储”
C。文件夹访问权限为“我的应用程序需要访问 Dropbox 上已有的文件”。(即:权限类型为“Full Dropbox”。)
然后单击“生成访问令牌”按钮并剪切/粘贴到下面的 python 示例中以代替
<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
回答by user94559
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).
对 Dropbox API 调用进行身份验证的唯一方法是使用 OAuth,这需要用户向您的应用授予权限。我们不允许第三方应用处理用户凭据(用户名和密码)。
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.
如果这仅适用于您的帐户,请注意,您可以轻松为自己的帐户获取 OAuth 令牌并使用它。请参阅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.
如果这适用于其他用户,他们需要通过浏览器对您的应用进行一次授权,以便您获得 OAuth 令牌。但是,一旦您拥有令牌,您就可以继续使用它,因此每个用户只需执行一次此操作。
回答by Caleb Connolly
Sorry if im missing something but cant you just download the dropbox application for your OS and then save the file (in windows) in:
抱歉,如果我遗漏了一些东西,但您不能为您的操作系统下载 Dropbox 应用程序,然后将文件(在 Windows 中)保存在:
C:\Users\<UserName>\Dropbox\<FileName>
i just ceated a python program to save a text file, checked my dropbox and it saves them fine.
我刚刚创建了一个 python 程序来保存一个文本文件,检查了我的保管箱,它保存得很好。
回答by jasminavanasiwala
Here is the code for uploading livevideo on dropbox using python in windows. Hope this will help you.
这是在 Windows 中使用 python 在 dropbox 上上传 livevideo 的代码。希望这会帮助你。
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
回答by jasminavanasiwala
Here is the code for uploading existing video on your dropbox account using python in windows.
这是在 Windows 中使用 python 在您的保管箱帐户上上传现有视频的代码。
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.
只写您要上传的图像文件名,例如: image.jpg 代替视频名称。还要更改 video1.avi 的名称并为图像写入名称,其中您上传的图像将显示在您的保管箱中,例如:image1.jpg。
回答by SparkAndShine
The answer of @Christinais based on Dropbox APP v1, which is deprecated now and will be turned off on 6/28/2017. (Refer to herefor more information.)
@Christina的答案基于 Dropbox APP v1,现在已弃用,将于2017 年 6月 28日关闭。(有关更多信息,请参阅此处。)
APP v2is launched in November, 2015 which is simpler, more consistent, and more comprehensive.
APP v2于2015年11月上线,更简单、更一致、更全面。
Here is the source code with APP v2.
这是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.
源代码托管在 GitHub 上,这里。
回答by Steve Lockwood
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.
这是我使用 API v2(和 Python 3)的方法。我想上传一个文件并为其创建一个共享链接,我可以通过电子邮件将其发送给用户。它基于 sparkandshine 的示例。注意我认为当前的 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)
回答by user7384403
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)