如何使用 python 和 Synology NAS API 将文件上传到 Synology NAS?

问题描述 投票:0回答:1

我正在尝试创建一个 python 脚本,以使用官方 Synology NAS API 和 python 请求模块将 xlsx 文件上传到 Synology NAS。我已经能够分别使用 SYNO.API.Auth 和 SYNO.FileStation.List api 成功登录并显示 filestation 信息,但由于某种原因,我无法让 SYNO.FileStation.Upload api 工作。这是我目前的尝试:

file_path = 'path to xlsx file'

# API endpoint for file upload
upload_url = f'{url}/webapi/entry.cgi'

# The file to be uploaded
files = {'file': (open(file_path, 'rb'))}

# Parameters for the file upload request
upload_params = {
    'api': 'SYNO.FileStation.Upload',
    'version': '2',
    'method': 'upload',
    'path': '/home/Drive',
    'create_parents': 'true',
    '_sid': sid,
}

try:
  # Sending the file upload request
  upload_response = requests.post(upload_url, params=upload_params, files=files)
  upload_data = upload_response.json()
except Exception as e:
  print(f'Error uploading file: {e}')

当我执行脚本时,它似乎在尝试发出 POST 请求时无限期地挂起。我是否遗漏了某些内容,或者我可以尝试使用哪些其他方法将文件上传到 Synology NAS?

python python-requests synology nas
1个回答
0
投票

将_sid放入params即可。

示例:

url = 'http://192.168.31.200:5000/webapi/entry.cgi'

params = {
'_sid': session_id,
}

data = {
'api': 'SYNO.FileStation.Upload',
'version': '2',
'method': 'upload',
'path': '/temp',
'create_parents': 'true',
'overwrite': 'true',
}

files = {'file': (open('123.txt', 'rb'))}

response = requests.post(url, params=params, data=data, files=files)
print(response.json())
© www.soinside.com 2019 - 2024. All rights reserved.