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

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

  1. 引入依赖

    
    <dependency>
        <groupId>com.alibaba.cloudgroupId>
        <artifactId>aliyun-oss-spring-boot-starterartifactId>
        <exclusions>
            
            <exclusion>
                <groupId>com.aliyungroupId>
                <artifactId>aliyun-java-sdk-coreartifactId>
            exclusion>
        exclusions>
    dependency>
    <dependency>
        
        <groupId>com.aliyungroupId>
        <artifactId>aliyun-java-sdk-coreartifactId>
        <version>4.5.0version>
    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.cloudgroupId>
                <artifactId>aliyun-spring-boot-dependenciesartifactId>
                <version>1.0.0version>
                <type>pomtype>
                <scope>importscope>
            dependency>
        dependencies>
    dependencyManagement>
    
  2. 在 gulimall-product 的配置文件中加上

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

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

你可能感兴趣的:(Gulimall,阿里云,java)