在gulimall的项目,需要导入aliyun的OSS-Starter,依据官方文档依次操作
aliyun-oss-java-sdk安装官方连接
在pom文件中导入新版的依赖:
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.10.2version>
dependency>
因为我的java版本是9以上的,所以还要导入一下依赖:
<dependency>
<groupId>javax.xml.bindgroupId>
<artifactId>jaxb-apiartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.glassfish.jaxbgroupId>
<artifactId>jaxb-runtimeartifactId>
<version>2.3.3version>
dependency>
这里就可以使用了,编写一个test测试一下,目前为止一切正常。
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// 云账号AccessKey有所有API访问权限,建议遵循阿里云安全最佳实践,创建并使用RAM子账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建。
String accessKeyId = "" ;
String accessKeySecret = "" ;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = new FileInputStream("" );
ossClient.putObject("" , "" , inputStream);
// 关闭OSSClient。
ossClient.shutdown();
但是,我比较懒,想用的是starter啊,去github上搜索它的使用例子
github OSS实例链接
找到公共依赖模块,编写它的pom文件,引入oss的starter依赖,在yaml文件中配置好自己的AccessKeyId、AccessKeySecret、endpoint:
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
dependency>
然后就报错了,如标题一样
Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
好吧,百度走起,看了看别人的解决方案,又添加了一些依赖:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-spring-boot-dependenciesartifactId>
<version>1.0.0version>
<type>pomtype>
<scope>importscope>
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
劳资辛辛苦苦解决依赖,竟然是你配置文件的锅。 (劳资裤子都脱了,你竟然给我看这个,不是)
最终我的得依赖如下
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-oss-spring-boot-starterartifactId>
<version>1.0.0version>
<exclusions>
<exclusion>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-ossartifactId>
exclusion>
<exclusion>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-coreartifactId>
<version>4.5.7version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>3.10.2version>
dependency>
<dependency>
<groupId>javax.xml.bindgroupId>
<artifactId>jaxb-apiartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>org.glassfish.jaxbgroupId>
<artifactId>jaxb-runtimeartifactId>
<version>2.3.3version>
dependency>
dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>2.2.3.RELEASEversion>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>aliyun-spring-boot-dependenciesartifactId>
<version>1.0.0version>
<type>pomtype>
<scope>importscope>
dependency>
dependencies>
dependencyManagement>
要不是折腾我这久我才写这个笔记,哼╭(╯^╰)╮
看到这里了,对你有没有一丢丢的帮助,点个赞吧,三克油
参考链接:
https://github.com/alibaba/aliyun-spring-boot/issues/40
https://blog.csdn.net/weixin_37056888/article/details/108953093