阿里云对象存储(OSS)服务
-
引入依赖
<!--阿里云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-keysecret-key: your-secret-keyoss:endpoint: your-endpoint
-
测试
@SpringBootTest class GulimallProductApplicationTests {@AutowiredOSSClient ossClient;@Testvoid 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("上传成功!");}}