OSS: (Object Storage Service) 对象存储服务
流程
开通
https://www.bilibili.com/video/BV1np4y1C7Yf?p=61&t=190.1
依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
</dependency>
配置文件
server:
port: 30000
spring:
application:
name: gulimall-third-party
cloud:
nacos:
discovery:
server-addr: 127.0.0.1:8848
alicloud:
access-key: xxx
secret-key: xxx
oss:
endpoint: oss-cn-shanghai.aliyuncs.com
bucket: gulimall-wangjq
应用服务器—获取签名信息接口
@Slf4j
@RestController
public class OssController {
@Resource
OSS ossClient;
@Value("${spring.cloud.alicloud.oss.endpoint}")
private String endpoint;
@Value("${spring.cloud.alicloud.oss.bucket}")
private String bucket;
@Value("${spring.cloud.alicloud.access-key}")
private String accessId;
@RequestMapping("/oss/policy")
public R policy() {
// 自己的oss域名地址 https://gulimall-wangjq.oss-cn-shanghai.aliyuncs.com
String host = "https://" + bucket + "." + endpoint;
// 用户上传文件时指定的前缀 文件目录 好管理 eg:2022-01-27/
String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
String dir = format + "/";
Map<String, String> respMap = null;
try {
// 设置Policy过期时间
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
// PostObject请求最大可支持的文件大小为5 GB,即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes("utf-8");
// 用户表单上传的策略(Policy),Policy为经过Base64编码过的字符串。
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
// 对Policy签名后的字符串。
// 下面方法会根据你配置信息中的secret-key来加密生成signature
// 如果密码填的不正确就无法与阿里云中心计算的签名进行匹配就不会给你上传
String postSignature = ossClient.calculatePostSignature(postPolicy);
respMap = new LinkedHashMap<String, String>();
respMap.put("accessid", accessId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
} catch (Exception e) {
log.error(e.getMessage());
} finally {
ossClient.shutdown();
}
return R.ok().put("data", respMap);
}
}
补充:secret-key填写错误返回的信息
<?xml version="1.0" encoding="UTF-8"?>
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
<RequestId>61F25861725B73343596E491</RequestId>
<HostId>gulimall-wangjq.oss-cn-shanghai.aliyuncs.com</HostId>
<OSSAccessKeyId>LTAI5tAyBWq1pEhSYc8g6gwd</OSSAccessKeyId>
<SignatureProvided>+LfK0coEUvTpA3TV+aDusJ2VaiA=</SignatureProvided>
<StringToSign>eyJleHBpcmF0aW9uIjoiMjAyMi0wMS0yN1QwODozMjowMS4wMjVaIiwiY29uZGl0aW9ucyI6W1siY29udGVudC1sZW5ndGgtcmFuZ2UiLDAsMTA0ODU3NjAwMF0sWyJzdGFydHMtd2l0aCIsIiRrZXkiLCIyMDIyLTAxLTI3LyJdXX0=</StringToSign>
<StringToSignBytes>65 79 4A 6C 65 48 42 70 63 6D 46 30 61 57 39 75 49 6A 6F 69 4D 6A 41 79 4D 69 30 77 4D 53 30 79 4E 31 51 77 4F 44 6F 7A 4D 6A 6F 77 4D 53 34 77 4D 6A 56 61 49 69 77 69 59 32 39 75 5A 47 6C 30 61 57 39 75 63 79 49 36 57 31 73 69 59 32 39 75 64 47 56 75 64 43 31 73 5A 57 35 6E 64 47 67 74 63 6D 46 75 5A 32 55 69 4C 44 41 73 4D 54 41 30 4F 44 55 33 4E 6A 41 77 4D 46 30 73 57 79 4A 7A 64 47 46 79 64 48 4D 74 64 32 6C 30 61 43 49 73 49 69 52 72 5A 58 6B 69 4C 43 49 79 4D 44 49 79 4C 54 41 78 4C 54 49 33 4C 79 4A 64 58 58 30 3D </StringToSignBytes>
</Error>
补充:calculatePostSignature方法获取secret-key
响应结果
{
"msg":"success",
"code":0,
"data":{
"accessid":"xxx",
"policy":"xxx",
"signature":"xxx",
"dir":"2022-01-27/",
"host":"https://gulimall-wangjq.oss-cn-shanghai.aliyuncs.com",
"expire":"1643269231"
}
}
用户/客服端/浏览器 根据签名信息发起请求
请求地址:https://gulimall-wangjq.oss-cn-shanghai.aliyuncs.com/
前端把签名接口返回的数据进行封装
_self.dataObj.policy = response.data.policy;
_self.dataObj.signature = response.data.signature;
_self.dataObj.ossaccessKeyId = response.data.accessid;
_self.dataObj.key = response.data.dir + ''+getUUID()+'_${filename}';
_self.dataObj.dir = response.data.dir;
_self.dataObj.host = response.data.host;
实际发送Body内容:
policy: xxx
signature: xxx
key: 2022-01-27/0138cf5d-4f79-4a40-8429-797a5a503773_${filename}
ossaccessKeyId: xxx
dir: 2022-01-27/
host: https://gulimall-wangjq.oss-cn-shanghai.aliyuncs.com
# 图片
file: (binary)
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="policy"
xxx
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="signature"
xxx
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="key"
2022-01-27/0138cf5d-4f79-4a40-8429-797a5a503773_${filename}
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="ossaccessKeyId"
xxx
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="dir"
2022-01-27/
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="host"
https://gulimall-wangjq.oss-cn-shanghai.aliyuncs.com
------WebKitFormBoundaryFA45Uv7O5oEASnn9
Content-Disposition: form-data; name="file"; filename="oppo.png"
Content-Type: image/png
------WebKitFormBoundaryFA45Uv7O5oEASnn9--