对象存储 #### 谷粒商城上传logo 创建第三方服务模块 修改pom文件 ?xml version="1.0" encoding="UTF-8"?project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocat
#### 谷粒商城上传logo
-
创建第三方服务模块
-
修改pom文件
-
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.8</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.cui</groupId> <artifactId>gulimall-thirdparty</artifactId> <version>0.0.1-SNAPSHOT</version> <name>gulimall-thirdparty</name> <description>gulimall-thirdparty</description> <properties> <java.version>11</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-configuration-processor</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>com.cui.gulimall-common</groupId> <artifactId>gulimall-common</artifactId> <version>0.0.1-SNAPSHOT</version> <exclusions> <exclusion> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> </exclusion> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-commons</artifactId> </exclusion> <exclusion> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-context</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.1.2</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>aliyun-oss-spring-boot-starter</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2021.0.1.0</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.2</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>aliyun-spring-boot-dependencies</artifactId> <version>1.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
-
-
修改配置文件
-
# 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<Map<String, String>> uploadFile() { Map<String, String> uploadToken = objectStorageService.upload(); return new Result<Map<String, String>>().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<String, String> 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<String, String> 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<String, String>(); 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==
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.1.2</version>
</dependency>
使用@ConfigurationProperties注解配置元数据需要导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>