第三方服务整合-阿里云OSS上传文件-填坑

如下,引入aliyun-oss失败:

com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown

原因:

在2.2.0.RELEASE以后的版本中,OSS依赖被转移到了alibaba/aliyun-spring-boot仓库中。需引入阿里云服务 Spring Boot Starters 工程的依赖管理。

解决方法


<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.0modelVersion>
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.2.1.RELEASEversion>
        <relativePath/> 
    parent>
    <groupId>cn.com.codingcegroupId>
    <artifactId>gulimall-third-partyartifactId>
    <version>0.0.1-SNAPSHOTversion>
    <name>gulimall-third-partyname>
    <description>第三方服务description>
    <properties>
        <java.version>1.8java.version>
        <spring-cloud.version>Hoxton.RELEASEspring-cloud.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>cn.com.codingcegroupId>
            <artifactId>gulimall-commonartifactId>
            <version>0.0.1-SNAPSHOTversion>
            
            <exclusions>
                <exclusion>
                    <groupId>com.baomidougroupId>
                    <artifactId>mybatis-plus-boot-starterartifactId>
                exclusion>
            exclusions>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>

        
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            
            <artifactId>spring-cloud-starter-alicloud-ossartifactId>
            <version>2.1.0.RELEASEversion>
        dependency>



    dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloudgroupId>
                <artifactId>spring-cloud-dependenciesartifactId>
                <version>${spring-cloud.version}version>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>spring-cloud-alibaba-dependenciesartifactId>
                <version>2.2.2.RELEASEversion>
                <type>pomtype>
                <scope>importscope>
            dependency>
            <dependency>
                <groupId>com.alibaba.cloudgroupId>
                <artifactId>aliyun-oss-spring-boot-starterartifactId>
                <version>1.0.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

project>

接着再配置文件中添加以下配置,执行程序后上传成功。

spring:
  application:
    name: gulimall-third-party

  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    alicloud:
      access-key: "LTAI4GEBfPJnto1Ut5sWozWa"
      secret-key: "sJxQJEGBIdwjrbsIz1xdd7MjaAZq6f"
      oss:
        endpoint: "oss-cn-beijing.aliyuncs.com"
        # 本没有这个属性, 自己添加
        bucket: "codingce-product"

logging:
  level:
    cn.com.codingce: debug

server:
  port: 30000

OssController

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 {
     

    @Autowired
    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/polcy")
    public Map<String, String> polcy() {
     
        // String accessId = ""; // 请填写您的AccessKeyId。
        // String accessKey = ""; // 请填写您的AccessKeySecret。
        // String endpoint = "oss-cn-hangzhou.aliyuncs.com"; // 请填写您的 endpoint。
        // String bucket = "codingce-product"; // 请填写您的 bucketname 。
        String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
        // callbackUrl为 上传回调服务器的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"; // 用户上传文件时指定的前缀。

        // 创建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;
    }

}

第三方服务整合-阿里云OSS上传文件-填坑_第1张图片
第三方服务整合-阿里云OSS上传文件-填坑_第2张图片

你可能感兴趣的:(操作,Spring)