对象存储
#### 谷粒商城上传logo
- 创建第三方服务模块
- 修改pom文件
–
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.6.8
com.cui
gulimall-thirdparty
0.0.1-SNAPSHOT
gulimall-thirdparty
gulimall-thirdparty
11
org.springframework.boot
spring-boot-configuration-processor
true
com.cui.gulimall-common
gulimall-common
0.0.1-SNAPSHOT
com.baomidou
mybatis-plus-boot-starter
org.springframework.cloud
spring-cloud-commons
org.springframework.cloud
spring-cloud-context
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-starter-bootstrap
3.1.2
org.springframework.cloud
spring-cloud-starter-openfeign
com.alibaba.cloud
aliyun-oss-spring-boot-starter
com.alibaba.cloud
spring-cloud-alibaba-dependencies
2021.0.1.0
pom
import
org.springframework.cloud
spring-cloud-dependencies
2021.0.2
pom
import
com.alibaba.cloud
aliyun-spring-boot-dependencies
1.0.0
pom
import
org.springframework.boot
spring-boot-maven-plugin
- 修改配置文件
–
nacos服务地址
spring.cloud.nacos.server-addr=192.168.215.137:8848
项目名
spring.application.name=thirdparty
端口号
server.port=30000
修改路径匹配规则 解决swagger冲突问题
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
–
服务名
spring.application.name=thirdparty
nacos服务地址
spring.cloud.nacos.config.server-addr=192.168.215.137:8848
命名空间
spring.cloud.nacos.config.namespace=d4100c2a-0991-4f7e-8e84-2db324678a0d
配置文件id
spring.cloud.nacos.config.extension-configs[0].data-id=gulimall_thirdparty.properties
开启同步更新配置
spring.cloud.nacos.config.extension-configs[0].refresh=true
#默认分组
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP
- 开启服务发现
- 在nacos中配置相关属性
- 创建命名空间
- 创建配置文件
- 在配置文件中添加AK等信息
alibaba.cloud.accessKey=LTAI5tKbSBpALoHC7JkBsF4g
alibaba.cloud.secretKey=AkpjpwVSqtiwQFyA66e2UAghxn6xK9
alibaba.cloud.oss.endpoint=oss-cn-beijing.aliyuncs.com
alibaba.cloud.bucket=exce-cui
- 创建接口
–
package com.cui.gulimallthirdparty.controller;
import com.cui.gulimall.common.utils.Result;
import com.cui.gulimallthirdparty.service.ObjectStorageService;
import com.cui.gulimallthirdparty.service.impl.ObjectStorageServiceImpl;
import io.swagger.annotations.Api;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
/**
* @author 崔令雨
* @date 2022/5/31 9:09
* @Version 1.0
*/
@RestController
@Api("上传文件控制")
@RequestMapping("thirdparty/oss")
public class ObjectStorageController {
private final ObjectStorageService objectStorageService;
@Autowired
public ObjectStorageController(ObjectStorageService objectStorageService) {
this.objectStorageService = objectStorageService;
}
@PostMapping("upload")
public Result> uploadFile() {
Map uploadToken = objectStorageService.upload();
return new Result>().ok(uploadToken);
}
}
- service层
–
package com.cui.gulimallthirdparty.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.cui.gulimallthirdparty.service.ObjectStorageService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* @author 崔令雨
* @date 2022/5/31 9:10
* @Version 1.0
*/
@Service
public class ObjectStorageServiceImpl implements ObjectStorageService {
private final OSS client;
@Autowired
public ObjectStorageServiceImpl(OSS client) {
this.client = client;
}
@Value("${alibaba.cloud.accessKey}")
private String accessKey;
@Value("${alibaba.cloud.secretKey}")
private String secretKey;
@Value("${alibaba.cloud.bucket}")
private String bucket;
@Value("${alibaba.cloud.oss.endpoint}")
private String endpoint;
@Override
public Map upload() {
// 填写Bucket名称,例如examplebucket。
// 填写Host地址,格式为https://bucketname.endpoint。
String host = "https://" + bucket + "." + endpoint;
// 设置上传回调URL,即回调服务器地址,用于处理应用服务器与OSS之间的通信。OSS会在文件上传完成后,把文件上传信息通过此回调URL发送给应用服务器。
String callbackUrl = "https://192.168.0.0:8888";
// 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。
String dir = LocalDate.now().toString();
Map respMap = null;
try {
long expireTime = 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
Date expiration = new Date(expireEndTime);
PolicyConditions policyConds = new PolicyConditions();
policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConds);
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
respMap = new LinkedHashMap();
respMap.put("accessid", accessKey);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
// respMap.put("expire", formatISO8601Date(expiration));
} catch (Exception e) {
// Assert.fail(e.getMessage());
System.out.println(e.getMessage());
}
return respMap;
}
}
- 前段代码 如果不双向绑定图片不回加载
需要导入依赖才能开启bootrap.properties==
org.springframework.cloud
spring-cloud-starter-bootstrap
3.1.2
使用@ConfigurationProperties注解配置元数据需要导入依赖
org.springframework.boot
spring-boot-configuration-processor
true
Original: https://www.cnblogs.com/cuiwnd/p/16330608.html
Author: 01cui
Title: 学习谷粒学院对象存储 — logo上传功能
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/715255/
转载文章受原作者版权保护。转载请注明原作者出处!