[笔记] 阿里云OSS存储

文章介绍了如何在Java项目中引入阿里云OSSSDK,通过SpringBoot的@ConfigurationProperties绑定OSS配置参数,并提供了一个工具类用于上传MultipartFile到阿里云OSS,包括获取上传路径和关闭OSSClient的过程。
摘要由CSDN通过智能技术生成
  1. 引入阿里云OSS存储服务依赖
  2. <!--OSS对象存储服务-->
    <dependency>
        <groupId>com.aliyun.oss</groupId>
        <artifactId>aliyun-sdk-oss</artifactId>
        <version>3.15.1</version>
    </dependency>
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>
    <dependency>
        <groupId>javax.activation</groupId>
        <artifactId>activation</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- no more than 2.3.3-->
    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.3</version>
    </dependency>
    <!--会自动识别被@ConfigurationProperties标识的Bean对象 [可选 不会影响程序运行]-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
    </dependency>
  3. 在yml配置文件里面设置OSS参数  这写参数可以登录自己的阿里云获取
  4. aliyun:
      oss:
        endpoint: ********写自己的访问路径******
        accessKeyId: ********写自己的Id******
        accessKeySecret: ********写自己的密钥******
        bucketName: ********写自己的bucket名字******
  5. 通过注入的方式,获取配置文件设置的OSS参数
  6. package com.tq.utils;
    
    import lombok.Data;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.stereotype.Component;
    
    @Data
    @Component
    @ConfigurationProperties(prefix = "aliyun.oss")
    public class aliyunProperties {
        private String endpoint;
        private String accessKeyId;
        private String accessKeySecret;
        private String bucketName;
    }
  7. 复制粘贴的OSS工具类
  8. package com.tq.utils;
    
    import com.aliyun.oss.OSS;
    import com.aliyun.oss.OSSClientBuilder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.UUID;
    
    /**
     * 阿里云 OSS 工具类
     */
    @Component
    public class AliOSSUtils {
    
        @Autowired
        private aliyunProperties aliyunProperties;
    
        /**
         * 实现上传图片到OSS
         */
        public String upload(MultipartFile file) throws IOException {
    //        获取阿里云OSS参数
            String endpoint = aliyunProperties.getEndpoint();
            String accessKeyId = aliyunProperties.getAccessKeyId();
            String accessKeySecret = aliyunProperties.getAccessKeySecret();
            String bucketName = aliyunProperties.getBucketName();
    
            // 获取上传的文件的输入流
            InputStream inputStream = file.getInputStream();
    
            // 避免文件覆盖
            String originalFilename = file.getOriginalFilename();
            String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
    
            //上传文件到 OSS
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
            ossClient.putObject(bucketName, fileName, inputStream);
    
            //文件访问路径
            String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
            // 关闭ossClient
            ossClient.shutdown();
            return url;// 把上传到oss的路径返回
        }
    
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值