ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

阿里云对象存储(OSS)服务

2026/8/10 20:26:45 拓冰建站 浏览量
阿里云对象存储(OSS)服务

阿里云对象存储(OSS)服务

  1. 引入依赖

    <!--阿里云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>
    
  2. 在 gulimall-product 的配置文件中加上

    alibaba:cloud:access-key: your-access-keysecret-key: your-secret-keyoss:endpoint: your-endpoint
    
  3. 测试

    @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("上传成功!");}}