首页 > 解决方案 > Onedrive API:上传特定目录中的文件

问题描述

我使用 python 和 Onedrive,我想将我的 onedrive 文件复制到特定目录中。

在 Onedrive 上,我的根空间中有以下目录和文件

1- /test/cdm
--> drive-api.py

2-  /test/cdm/data
--> tue.xlsx

3- /all-data

我想将tue.xlsx/test/cdm/data复制 到/all-data

我在 drive-api.py 中使用此代码

import onedrivesdk
from onedrivesdk.helpers import GetAuthCodeServer
from onedrivesdk.helpers.resource_discovery import ResourceDiscoveryRequest

redirect_uri = 'http://localhost:5000/login/authorized'
client_id = 'xxxxxxxxx'
client_secret = 'yyyyyyyyy'
discovery_uri = 'https://api.office.com/discovery/'
auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize'
auth_token_url='https://login.microsoftonline.com/common/oauth2/token'

http = onedrivesdk.HttpProvider()
auth = onedrivesdk.AuthProvider(http,
                                client_id,
                                auth_server_url=auth_server_url,
                                auth_token_url=auth_token_url)
auth_url = auth.get_auth_url(redirect_uri)
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
auth.authenticate(code, redirect_uri, client_secret, resource=discovery_uri)
# If you have access to more than one service, you'll need to decide
# which ServiceInfo to use instead of just using the first one, as below.
service_info = ResourceDiscoveryRequest().get_service_info(auth.access_token)[0]
auth.redeem_refresh_token(service_info.service_resource_id)
client = onedrivesdk.OneDriveClient(service_info.service_resource_id + '/_api/v2.0/', auth, http)

此代码工作正常,但它不是正确的目录

returned_item = client.item(drive='me', id='root').children['tue-copie.xlsx'].upload('data/tue.xlsx')

问题:如何指定 /all-data 目录?

感谢您提前提供任何帮助

标签: pythonpython-3.xapixlsxonedrive

解决方案


您几乎拥有它,您只需要在 client.item()中id更改path

所以假设文件夹被调用all-data,你可以像这样上传它:

returned_item = client.item(drive='me', path='all-data').children['sample.xlsx'].upload('sample.xlsx')

推荐阅读