python上传文件到oss_解决Python向阿里云OSS上传文件时报错400

如题,该问题并不是100%出现。出错时服务器返回信息如下:<?xml version="1.0" encoding="UTF-8"?>

<Error>

<Code>InvalidArgument</Code>

<Message>The bucket POST must contain the specified 'key'. If it is specified, please check the order of the fields</Message>

<RequestId>C21E709A5EC76E5ED07C0D43</RequestId>

<HostId>oss.example.com</HostId>

<ArgumentName>key</ArgumentName>

<ArgumentValue></ArgumentValue>

</Error>

官网文档对 The bucket POST must contain the specified ‘key’. If it is specified, please check the order of the fields 解释如下:

那么问题就明确了:由于在POST时使用了字典,其中的顺序并不是固定的。当key键排在file键后面时错误便会出现。解决方法也很简单,使用collections.OrderedDict。它是有序字典,可以明确键值对的添加顺序:import collections

content  = collections.OrderedDict()

content ['key'] = 'file_key'

...

content ['file'] = your_file

这样提交之后键值的顺序便会固定,不再出现上述问题。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值