阿里云 oss 之图片处理持久化

摘要:有时需要对客户传上来的图片做缩略图,LZ是对一张图片进行一次post请求后,就永久保存该缩略图,有用户请求,就返回该缩略图的URL

参考:
图片缩略图 https://help.aliyun.com/document_detail/44688.html?spm=a2c4g.11186623.6.970.ObfCov
数据处理持久化 https://help.aliyun.com/document_detail/55811.html?spm=a2c4g.11186623.6.995.CxbjKQ

注意:前期你得把你oss后台 AccessKey的这个权限得开起来
AliyunOSSFullAccess
参考 https://help.aliyun.com/document_detail/28652.html?spm=a2c4g.11186623.6.563.6sKs3g

LZ为了方便,直接浏览器插件进行的post请求,比如postman等,请求的参数配置如下
这里写图片描述

返回结果如下

这里写图片描述

上面LZ写在headers里的3个参数,还有很多其他的参数,具体的自己看文档,时间格式是GMT的,Java生成GMT格式时间

 public static void main(String args[]) {
         Calendar calendar = Calendar.getInstance();
         SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
         sdf.setTimeZone(TimeZone.getTimeZone("GMT")); // 设置时区为GMT Tue, 20 Mar 2018 09:00:31 GMT
         String time = sdf.format(calendar.getTime());
         System.out.println(time);
     }

至于这个 Authorization,如是oss后台管理的bucket的权限是公共读写,就不需要传参数也可访问成功,如是设置了权限,就得填写,Authorization生成可以查看
官网文档 https://help.aliyun.com/document_detail/31951.html?spm=a2c4g.11186623.6.868.vkNQkd
在这里LZ就讲解 Signature(生成方式有很多)的生成代码,官网给的是python代码,运行环境是python2,代码

import base64
import hmac
import sha
h = hmac.new("accessKeySecret",
             "POST\n\napplication/x-www-form-urlencoded\nTue, 20 Mar 2018 02:59:45 GMT\nx-oss-date:Tue, 20 Mar 2018 02:59:45 GMT\n/bucketname/5.jpg?x-oss-process", sha)
Signature = base64.b64encode(h.digest())
print("Signature: %s" % Signature)

看仔细了,LZ的和官网代码的时间传的有点不一样,反正LZ按官网的python代码,死活没有成功,具体你们看上面给的文档

写的很匆忙,有不对的望留言

猜你喜欢

转载自blog.csdn.net/fengchen0123456789/article/details/79628539