导入OSS依赖不兼容问题
Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
在gulimall的项目,需要导入aliyun的OSS-Starter,依据官方文档依次操作
aliyun-oss-java-sdk安装官方连接
在pom文件中导入新版的依赖:
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
因为我的java版本是9以上的,所以还要导入一下依赖:
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
这里就可以使用了,编写一个test测试一下,目前为止一切正常。
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = "<yourAccessKeyId>";
String accessKeySecret = "<yourAccessKeySecret>";
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = new FileInputStream("<yourlocalFile>");
ossClient.putObject("<yourBucketName>", "<yourObjectName>", inputStream);
// 关闭OSSClient。
ossClient.shutdown();
但是,我比较懒,想用的是starter啊,去github上搜索它的使用例子
github OSS实例链接
找到公共依赖模块,编写它的pom文件,引入oss的starter依赖,在yaml文件中配置好自己的AccessKeyId、AccessKeySecret、endpoint:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
</dependency>
然后就报错了,如标题一样
Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
好吧,百度走起,看了看别人的解决方案,又添加了一些依赖:
<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>
<!-- oss依赖 解决unknown -->
<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>
心想,按照浆糊规矩,应该到此为止了,但是这个依赖它不讲武德,偷袭我一个20多岁的老年人,反手就是一个bug,我大意了,没有闪:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘ossClient’ defined in class path resource [com/alibaba/cloud/spring/boot/oss/autoconfigure/OssContextAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.aliyun.oss.OSS]: Factory method ‘ossClient’ threw exception; nested exception is java.lang.IllegalArgumentException: Oss endpoint can’t be empty.
它竟然说我没有指定endpoint,气得我直接去yml文件又重新检查了一遍
alicloud:
access-key: xxxxxxxxxxxxx
secret-key: xxxxxxxxxxxxx
oss:
endpoint: xxxxxxxxxxxx
命名好好的躺在那里但是它就是报错,我甚至还去它的OSS类里面吧这个方法看了一遍,确实,“参数没有问题”。又来回捣鼓,开始重新解决依赖。后来看到了一个博客,终于发现盲点,原来是oss版本的问题,参数确实有,但是格式错了,因为使用的IDEA的自动补全,所以很有信心,但是没成想还是错付了。
#新版写法
alicloud:
access-key: xxxxxxxxxxx
secret-key: xxxxxxxxxxx
oss:
endpoint: xxxxxxxxxxx
#老版写法
alibaba:
cloud:
access-key: xxxxxxxxxxx
secret-key: xxxxxxxxxxx
oss:
endpoint: xxxxxxxxxxx
一定要根据自己配置选用合适的格式
劳资辛辛苦苦解决依赖,竟然是你配置文件的锅。 (劳资裤子都脱了,你竟然给我看这个,不是)
最终我的得依赖如下
<!--oss依赖-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<version>1.0.0</version>
<exclusions>
<!-- 排除aliyun-java-sdk-oss 版本过低 -->
<exclusion>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-oss</artifactId>
</exclusion>
<!-- 排除aliyun-sdk-oss,版本太低,是3.1版本的-->
<exclusion>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 导入新的依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.7</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
</dependency>
<!-- no more than 2.3.3-->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.3</version>
</dependency>
</dependencies>
<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>
<!-- oss依赖-->
<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>
要不是折腾我这久我才写这个笔记,哼╭(╯^╰)╮
看到这里了,对你有没有一丢丢的帮助,点个赞吧,三克油
参考链接:
https://github.com/alibaba/aliyun-spring-boot/issues/40
https://blog.csdn.net/weixin_37056888/article/details/108953093
本文地址:https://blog.csdn.net/weixin_44320727/article/details/110213661
推荐阅读