SpringBoot版本:2.7.12
SpringCloud版本:2021.0.7
SpringCloud-Alibaba版本:2021.0.5.0
文章的两种整合方式主要是以下两点的不同,此外我在配置过程中碰到的错误将在具体的整合方式里说明
1.引入的依赖的不同
<!--原来的-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
</dependency>
<!--现在的-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
</dependency>
2.application.yml文件的配置不同
#原来的
spring:
cloud:
alicloud:
access-key: yourkey
secret-key: yourkey
oss:
endpoint: ....
#现在的
alibaba:
cloud:
access-key: yourkey
secret-key: yourkey
oss:
endpoint: ....
整合方式一
和视频引入的包相同,在common模块的pom.xml引入下面依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alicloud-oss</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
注意要带上<version>,否则报错:
Unresolved dependency: 'com.alibaba.cloud:spring-cloud-starter-alicloud-oss:jar:unknown'
在gulimall-product模块的application.yml文件进行如下配置
spring:
cloud:
alicloud:
access-key: XXX
secret-key: XXX
oss:
endpoint: oss-cn-beijing.aliyuncs.com
util:
enabled: false
重点:需要配置spring.cloud.util:enabled为false,否则如下报错
***************************
APPLICATION FAILED TO START
***************************Description:
Parameter 0 of method inetIPv6Utils in com.alibaba.cloud.nacos.util.UtilIPv6AutoConfiguration required a single bean, but 2 were found:
- spring.cloud.inetutils-org.springframework.cloud.commons.util.InetUtilsProperties: defined in unknown location
- inetUtilsProperties: defined by method 'inetUtilsProperties' in class path resource [org/springframework/cloud/commons/util/UtilAutoConfiguration.class]
Action:Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed
和视频一样测试代码是否可行
// 我这里不用@RunWith也可以运行起来
//@RunWith(SpringRunner.class)
@SpringBootTest
class GulimallProductApplicationTests {
@Autowired
BrandService brandService;
// 我这里用Autowired虽然会标红,但是可以运行起来
// 也可以使用@Resource
@Autowired
OSSClient ossClient;
@Test
public void testUpload() throws FileNotFoundException {
// 通过client发起上传文件的请求
String bulkName ="gulimall-XXXX";
String objectName= "abc123.png"; //文件的唯一标识,通常是不重复的文件名
InputStream in = new FileInputStream("C:\\Users\\laptop\\Pictures\\Saved Pictures\\abc123.png");
// 上传文件到指定的存储空间(bucketName)并将其保存为指定的文件名称(key)。
ossClient.putObject(bulkName, objectName, in);
// 关闭连接
ossClient.shutdown();
}
}
整合方式二
用aliyun-spring-boot官网示例的依赖,在common模块的pom.xml引入下面依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<!--带上版本号-->
<version>1.0.0</version>
</dependency>
<!--导入新的依赖-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.5</version>
</dependency>
注意:我这里比aliyun-spring-boot官网示例多引入aliyun-java-sdk-core,不引入会报以下错误,具体可以参考整合OSS依赖导入后启动报错这篇文章。
***************************
APPLICATION FAILED TO START
***************************Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
com.alibaba.cloud.context.AliCloudSdk.<init>(AliCloudSdk.java:76)
The following method did not exist:
com.aliyuncs.profile.DefaultProfile.getHttpClientConfig()Lcom/aliyuncs/http/HttpClientConfig;
The calling method's class, com.alibaba.cloud.context.AliCloudSdk, was loaded from the following location:
jar:file:/E:/maven_repositories/repository/com/alibaba/cloud/alicloud-context/1.0.5/alicloud-context-1.0.5.jar!/com/alibaba/cloud/context/AliCloudSdk.class
The called method's class, com.aliyuncs.profile.DefaultProfile, is available from the following locations:
jar:file:/E:/maven_repositories/repository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar!/com/aliyuncs/profile/DefaultProfile.class
The called method's class hierarchy was loaded from the following locations:
com.aliyuncs.profile.DefaultProfile: file:/E:/maven_repositories/repository/com/aliyun/aliyun-java-sdk-core/3.4.0/aliyun-java-sdk-core-3.4.0.jar
Action:Correct the classpath of your application so that it contains compatible versions of the classes com.alibaba.cloud.context.AliCloudSdk and com.aliyuncs.profile.DefaultProfile
在gulimall-product模块的application.yml文件进行如下配置(注意:和视频的配置不同,注意区分)
alibaba:
cloud:
access-key: XXX
secret-key: XXX
oss:
endpoint: oss-cn-beijing.aliyuncs.com
和视频一样测试代码是否可行(和整合方式一代码相同)
// 我这里不用@RunWith也可以运行起来
//@RunWith(SpringRunner.class)
@SpringBootTest
class GulimallProductApplicationTests {
@Autowired
BrandService brandService;
// 我这里用Autowired虽然会标红,但是可以运行起来
// 也可以使用@Resource
@Autowired
OSSClient ossClient;
@Test
public void testUpload() throws FileNotFoundException {
// 通过client发起上传文件的请求
String bulkName ="gulimall-XXXX";
String objectName= "abc123.png"; //文件的唯一标识,通常是不重复的文件名
InputStream in = new FileInputStream("C:\\Users\\laptop\\Pictures\\Saved Pictures\\abc123.png");
// 上传文件到指定的存储空间(bucketName)并将其保存为指定的文件名称(key)。
ossClient.putObject(bulkName, objectName, in);
// 关闭连接
ossClient.shutdown();
}
}