解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown 檔案上傳報錯aliCloudEdasSdk解決

cevent發表於2020-10-08

1.解決unknown:

<!--引入spring cloud alibaba-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.2.3.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown-->
            <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>

2.執行上傳測試報錯

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘com.cevent.yameng.web.mall.product.YamengMallProductApplicationTests’: Unsatisfied dependency expressed through field ‘ossClient’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘com.aliyun.oss.OSSClient’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)

alibaba

3.問題分析:阿里雲dependencies版本不相容

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aliCloudEdasSdk' defined in class path resource [com/alibaba/cloud/spring/boot/context/autoconfigure/EdasContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.alibaba.cloud.context.edas.AliCloudEdasSdk]: Factory method 'aliCloudEdasSdk' threw exception; nested exception is java.lang.NoSuchMethodError: com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;

4.修改common-pom

aliyun-cevent

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>yameng-mall</artifactId>
        <groupId>com.cevent.yameng.web.mall</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>yameng-mall-common</artifactId>
    <description>各個微服務系統依賴的公共服務包,包括bean、utils等工具類</description>

    <dependencies>
        <!--引入mybatis-plus-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.3.1</version>
        </dependency>
        <!--引入lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.8</version>
        </dependency>
        <!--引入httpStatus依賴的java傳送http請求元件-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.13</version>
        </dependency>
        <!--引入stringUtils需要的依賴-->
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <!--引入mysql驅動-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.17</version>
        </dependency>
        <!--引入http servlet包依賴,tomcat一般都有,所有這裡設定為scope=provided,打包時候不需要-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <!--引入ali-Nacos服務發現中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <!--<version>2.2.0.RELEASE</version>-->
        </dependency>
        <!--引入ali-Nacos服務配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--引入阿里雲封裝好的cloud oss-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
        </dependency>

    </dependencies>

    <!--引入spring cloud alibaba-->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <!--<version>2.2.3.RELEASE</version>-->
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--解決Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown-->
            <!--<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>

</project>

5.Product.pom繼續使用之前的oss,可省略,只為測試原生上傳

<?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.3.3.RELEASE</version>
        <!--<version>2.1.8.RELEASE</version>-->
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.cevent.yameng.web.mall</groupId>
    <artifactId>yameng-mall-product</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>yameng-mall-product</name>
    <description>亞盟商城-商品服務模組</description>

    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

    <dependencies>
        <!--新增common公共模組依賴-->
        <dependency>
            <groupId>com.cevent.yameng.web.mall</groupId>
            <artifactId>yameng-mall-common</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--引入阿里雲物件儲存,後期用alibaba cloud oss 可省略-->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </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>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </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>
        </dependencies>
    </dependencyManagement>

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

</project>

6. Product.yml配置阿里雲access-key、secret-key、endpoint

cevent
cevent

7.yml配置詳情

spring:
  datasource:
    username: root
    password: cevent
    url: jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.jdbc.Driver
  #4.配置nacos註冊中心
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
    #5.配置阿里巴巴-cloud-oss,這裡對應common.pom的spring-cloud-alibaba-dependencies(2.1.0.RELEASE),不相容
    alicloud:
      access-key: ***
      secret-key: ***
      oss:
        endpoint: ***
##1.mapper-locations預設值: private String[] mapperLocations = new String[]{"classpath*:/mapper/**/*.xml"};
#classpath*:掃描包括本product系統及各個引用包下的類路徑
#classpath:只掃描本product系統
mybatis-plus:
  mapper-locations: classpath*:/mapper/**/*.xml
  #2.調整entity實體類的tableId為自增主鍵,資料量大後,進行分庫分表的主鍵生成規則/自定義生成規則
  global-config:
    db-config:
      id-type: auto
      # mybatis全域性邏輯刪除:1代表刪除 0代表沒有刪除
      logic-delete-value: 1
      logic-not-delete-value: 0

#3.配置啟動埠
server:
  port: 6622
#4.設定列印日誌
logging:
  level:
    com.cevent.yameng.web.mall: debug
#5.配置阿里巴巴-cloud-oss,這裡是新版本aliyun的配置,不相容
#alibaba:
#  cloud:
#    access-key: ***
#    secret-key: ***
#    oss:
#      endpoint: ***


8.test測試

package com.cevent.yameng.web.mall.product;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cevent.yameng.web.mall.product.entity.BrandEntity;
import com.cevent.yameng.web.mall.product.service.BrandService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;

/**
 * 測試brand-service
 */

@SpringBootTest
class YamengMallProductApplicationTests {

    @Autowired
    BrandService brandService;

    //1.測試新增品牌
    @Test
    void contextLoads() {
        BrandEntity brandEntity = new BrandEntity();
        brandEntity.setName("亞盟");
        brandService.save(brandEntity);
        System.out.println("儲存的brand-entity:" + brandEntity.getName());
    }

    //2.修改品牌
    @Test
    void updateBrand() {
        BrandEntity brandEntity = new BrandEntity();
        //id為long
        brandEntity.setBrandId(1L);
        brandEntity.setDescript("亞盟電子商務有限公司");
        brandService.updateById(brandEntity);
        System.out.println("更新的brand-entity:" + brandEntity.getDescript());
    }

    //3.查詢品牌
    @Test
    void selectBrand() {
        List<BrandEntity> brandEntityList;
        //包裝查詢wrapper,根據指定id查詢集合
        brandEntityList = brandService.list(new QueryWrapper<BrandEntity>().eq("brand_id", 1L));
        //遍歷集合
        brandEntityList.forEach((item) -> {
            System.out.println("查詢的brand-entity:" + item);
        });
    }

    //4.oss檔案上傳測試,這裡與OSSClient衝突
    @Test
    void testUpload() throws FileNotFoundException {
        // Endpoint以杭州為例,其它Region請按實際情況填寫。
        String endpoint = "endpoint";
        // 雲賬號AccessKey有所有API訪問許可權,建議遵循阿里雲安全最佳實踐,
        // 建立並使用RAM子賬號進行API訪問或日常運維,請登入 https://ram.console.aliyun.com 建立。
        String accessKeyId = "ak";
        String accessKeySecret = "sk";

        // 建立OSSClient例項。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        // 上傳檔案流
        //1.指定檔案
        InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\1_marknelson_anotherplace_t.jpg");
        //2.上傳到bucket
        ossClient.putObject("cevent-yameng-shop", "cevent_icon.jpg", inputStream);

        // 關閉OSSClient。
        ossClient.shutdown();

        System.out.println("OSS上傳完成!");
    }

    @Autowired
    OSSClient ossClient;
    //5.alibaba-cloud-oss檔案上傳測試
    @Test
    void testCloudOSSUpload() throws FileNotFoundException {

        // 上傳檔案流
        //1.指定檔案
        InputStream inputStream = new FileInputStream("D:\\CEVENT_INFO\\icon1.jpg");
        //2.上傳到bucket
        ossClient.putObject("cevent-yameng-shop", "cevent_red2.jpg", inputStream);

        // 關閉OSSClient。
        ossClient.shutdown();

        System.out.println("aliyun-OSS上傳完成!");
    }

}

cevent
cevent

相關文章