第三方服务整合-阿里云OSS上传文件-填坑
生活随笔
收集整理的這篇文章主要介紹了
第三方服务整合-阿里云OSS上传文件-填坑
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
目錄
- 如下,引入aliyun-oss失敗:
- 原因:
- 解決方法
- OssController
如下,引入aliyun-oss失敗:
com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
原因:
在2.2.0.RELEASE以后的版本中,OSS依賴被轉(zhuǎn)移到了alibaba/aliyun-spring-boot倉庫中。需引入阿里云服務 Spring Boot Starters 工程的依賴管理。
解決方法
<?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.2.1.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>cn.com.codingce</groupId><artifactId>gulimall-third-party</artifactId><version>0.0.1-SNAPSHOT</version><name>gulimall-third-party</name><description>第三方服務</description><properties><java.version>1.8</java.version><spring-cloud.version>Hoxton.RELEASE</spring-cloud.version></properties><dependencies><!-- 公眾項目 --><dependency><groupId>cn.com.codingce</groupId><artifactId>gulimall-common</artifactId><version>0.0.1-SNAPSHOT</version><!--排除包--><exclusions><exclusion><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId></exclusion></exclusions></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--引入spring-cloud-starter-alicloud-oss--><!--導入失敗,需指定為2.1.0RELEASE穩(wěn)定版--><dependency><groupId>com.alibaba.cloud</groupId><!--<artifactId>aliyun-oss-spring-boot-starter</artifactId>--><artifactId>spring-cloud-starter-alicloud-oss</artifactId><version>2.1.0.RELEASE</version></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>2.2.2.RELEASE</version><type>pom</type><scope>import</scope></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>aliyun-oss-spring-boot-starter</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>接著再配置文件中添加以下配置,執(zhí)行程序后上傳成功。
spring:application:name: gulimall-third-partycloud:nacos:discovery:server-addr: 127.0.0.1:8848alicloud:access-key: "LTAI4GEBfPJnto1Ut5sWozWa"secret-key: "sJxQJEGBIdwjrbsIz1xdd7MjaAZq6f"oss:endpoint: "oss-cn-beijing.aliyuncs.com"# 本沒有這個屬性, 自己添加bucket: "codingce-product"logging:level:cn.com.codingce: debugserver:port: 30000OssController
package cn.com.codingce.thirdparty.controller;import com.aliyun.oss.OSS; import com.aliyun.oss.common.utils.BinaryUtil; import com.aliyun.oss.model.MatchMode; import com.aliyun.oss.model.PolicyConditions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.text.SimpleDateFormat; import java.util.Date; import java.util.LinkedHashMap; import java.util.Map;@RestController public class OssController {@AutowiredOSS 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/polcy")public Map<String, String> polcy() {// String accessId = "<yourAccessKeyId>"; // 請?zhí)顚懩腁ccessKeyId。// String accessKey = "<yourAccessKeySecret>"; // 請?zhí)顚懩腁ccessKeySecret。// String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 請?zhí)顚懩?endpoint。// String bucket = "codingce-product"; // 請?zhí)顚懩?bucketname 。String host = "https://" + bucket + "." + endpoint; // host的格式為 bucketname.endpoint// callbackUrl為 上傳回調(diào)服務器的URL,請將下面的IP和Port配置為您自己的真實信息。//String callbackUrl = "http://88.88.88.88:8888";String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());// String dir = "user-dir-prefix/"; // 用戶上傳文件時指定的前綴。String dir = "format"; // 用戶上傳文件時指定的前綴。// 創(chuàng)建OSSClient實例。// OSS ossClient = new OSSClientBuilder().build(endpoint, accessId, accessKey);Map<String, String> respMap = null;try {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");String encodedPolicy = BinaryUtil.toBase64String(binaryData);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));// respMap.put("expire", formatISO8601Date(expiration));} catch (Exception e) {// Assert.fail(e.getMessage());System.out.println(e.getMessage());} finally {ossClient.shutdown();}return respMap;}}
文章已上傳:https://gitee.com/codingce/hexo-blog
項目地址:https://github.com/xzMhehe/codingce-java
總結(jié)
以上是生活随笔為你收集整理的第三方服务整合-阿里云OSS上传文件-填坑的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Vue完整总结
- 下一篇: 基于前后端分离实现阿里云对象存储小案例