引入依赖
<!--阿里云OSS服务-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>aliyun-oss-spring-boot-starter</artifactId>
<exclusions>
<!--排除默认版本的依赖-->
<exclusion>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<!--引入4.5.0 版本依赖-->
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.0</version>
</dependency>
这里aliyun-oss-spring-boot-starter
中默认引入的 aliyun-java-sdk-core
是 3.4.0 版本,但是 aliyun-spring-boot-dependencies
中对 aliyun-java-sdk-core
版本管理为:4.5.0,会导致版本冲突
所以排除 aliyun-oss-spring-boot-starter
默认的 aliyun-java-sdk-core
,单独引入 4.5.0 版本的 aliyun-java-sdk-core
如果提示aliyun-oss-spring-boot-starter
版本不能为空就加上依赖管理:
<dependencyManagement>
<dependencies>
<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>
在 gulimall-product 的配置文件中加上
alibaba:
cloud:
access-key: your-access-key
secret-key: your-secret-key
oss:
endpoint: your-endpoint
测试
@SpringBootTest
class GulimallProductApplicationTests {
@Autowired
OSSClient ossClient;
@Test
void testUpload() throws IOException {
String bucketName = "gulimall-xxx";
InputStream inputStream = Files.newInputStream(Paths.get("D:\\sources\\zip\\Guli Mall\\分布式基础\\资源\\pics\\0d40c24b264aa511.jpg"));
ossClient.putObject(bucketName, "test.jpg", inputStream);
System.out.println("上传成功!");
}
}