上传文件至华为云obs的签名不匹配的问题

背景:由于不想让用户查询到我的secretKey和accesskey,所以想在后端做一个临时url生成接口。
问题:在使用临时url上传文件到华为云obs的时候遇到403 forbidden问题,在浏览器开发者模式的网络请求记录里面还看不到返回响应里面的具体报错信息,需要双击网络请求,再访问一次才能看到一个xml的报错信息,具体报错信息是:

The request signature we calculated does not match the signature you provided. Check your key and signing method

分析:后来在createTemporarySignature方法的源码中看到,在签名的计算中不仅用到了url、时间、还有其头相关信息:

StringBuilder canonicalRequest = (new StringBuilder(requestMethod)).append("\n").append((CharSequence)(canonicalUri.length() == 0 ? "/" : canonicalUri)).append("\n").append(canonicalQueryString).append("\n").append(canonicalHeaders).append("\n").append(signedHeaders).append("\n").append("UNSIGNED-PAYLOAD");
StringBuilder stringToSign = (new StringBuilder("AWS4-HMAC-SHA256")).append("\n").append(longDate).append("\n").append(shortDate).append("/").append("region").append("/").append("s3").append("/").append("aws4_request").append("\n").append(V4Authentication.byteToHex(V4Authentication.sha256encode(canonicalRequest.toString())));
signedUrl.append("&").append("X-Amz-").append("Signature=").append(V4Authentication.caculateSignature(stringToSign.toString(), shortDate, securityKey.getSecretKey()));
TemporarySignatureResponse response = new TemporarySignatureResponse(signedUrl.toString());

这时候就想到了,我为了做临时url能否访问的测试,在生成临时url时候,content-type:写死的image/png,而我实际上传文是image/jpeg,因此就发现了问题,把content-type统一,上传成功!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值