我正在尝试使用python将包二进制文件上传到存储的RestAPI。但它不断抛出错误,无法上传文件。下面是我用来实现它的代码:
jsonheaderup={'Content-Type': 'application/octet-stream'}
file = open('install.pkg.gz', 'rb')
files = {'file': file}
def upload_code():
u = requests.post("%s/api/sys/v2/updates" % (url), files=files, verify=False, headers=jsonheaderup)
l = json.loads(u.text)
upload_code()
乍一看,我看不出任何错误。
你看到了这个:Python 3 script to upload a file to a REST URL (multipart request)?
早期的帖子并没有真正帮助,但我通过引用原始请求文档 - 流式上传来解决这个问题。 http://docs.python-requests.org/en/master/user/advanced/
由于我的文件大约为1.9 GB,因此会话在上传过程之间中断,因此将错误称为“内部错误”。
作为我的庞大文件,我通过在我的函数中提供类似文件的对象进行流式传输和上传:
def upload_code():
jsonheaderup={'Content-Type': 'application/octet-stream'}
with open('/root/ak-nas-2013-06-05-7-18-1-1-3-nd.pkg.gz', 'rb') as file:
requests.post("%s/api/system/v1/updates" % (url), data=file, auth=zfsauth, verify=False, headers=jsonheaderup, timeout=None)