Springboot整合阿里云oss存储服务

一、开通“对象存储OSS”服务

登录阿里云搜索oss服务,并开通
在这里插入图片描述
在这里插入图片描述

二. 使用RAM子用户

创建一个子用户用来管理
在这里插入图片描述
在这里插入图片描述
创建用户并设置权限
在这里插入图片描述
添加可操作的服务权限给创建的用户
在这里插入图片描述
创建Accesskey 需要在后面使用到
在这里插入图片描述

三 项目运用

使用SDK
在这里插入图片描述
在这里插入图片描述

四 项目创建

1.pom文件

        <!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.10.2</version>
        </dependency>
        
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

 

2.application.properties 配置文件

#阿里云oss存储系统
aliyun.oss.accessKeyId=“你的accesskeyId”
aliyun.oss.accessKeySecret="你的accessKeySecret"
aliyun.oss.endpoint=oss-cn-shanghai.aliyuncs.com

3.读取文件

@Data
@Component
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties implements InitializingBean {

    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;


    public static String ENDPOINT;
    public static String KEY_ID;
    public static String KEY_SECRET;

    //当私有成员被赋值后,此方法自动被调用,从而初始化常量
    @Override
    public void afterPropertiesSet() throws Exception {
        ENDPOINT = endpoint;
        KEY_ID = accessKeyId;
        KEY_SECRET = accessKeySecret;
    }

}

4.代码编写测试

import com.aliyun.oss.OSS;

import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.CannedAccessControlList;
import org.joda.time.DateTime;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import top.javahai.chatroom.config.OssProperties;


import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;


@RestController
@RequestMapping("file")
public class FileController {


  @PostMapping("/file")
  public String uploadFlie(@RequestParam("file") MultipartFile file,
                           @RequestParam("model")String model) throws IOException {

        try {
      InputStream inputStream = file.getInputStream();
      String originalFilename = file.getOriginalFilename();
      String url = upload(inputStream, model, originalFilename);
      return url;
    } catch (IOException e) {
            System.out.println(e.getMessage());
      return e.getMessage();
    }

  }


  private String upload(InputStream inputStream,String model,String fileName){

      //创建OssClient实例
      OSS ossClient = new OSSClientBuilder().build(
              OssProperties.ENDPOINT,
              OssProperties.KEY_ID,
              OssProperties.KEY_SECRET
      );
      //判断oss实例是否存在,如果不存在则创建,如果存在则获取
      if(!ossClient.doesBucketExist(model)){
          //创建bucket
          ossClient.createBucket(model);
          //设置oss实例的访问权限
          ossClient.setBucketAcl(model, CannedAccessControlList.PublicRead);
      }
      //构建日期路径
     String folder= new DateTime().toString("yyyy/MM/dd");
      //文件名:uuid.扩展名
      fileName=UUID.randomUUID().toString()+fileName.substring(fileName.lastIndexOf("."));
      //文件的根路径
      String key=model+"/"+folder+"/"+fileName;
      //文件上传阿里云
      ossClient.putObject(model,key,inputStream);
      //
      ossClient.shutdown();
      return "https://"+model+"."+OssProperties.ENDPOINT+"/"+key;

  }

结果;在这里会显示你所创建以及上传的文件
在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot可以通过整合阿里云OSS对象存储服务来实现文件上传和管理功能。具体实现可以参考以下步骤: 1. 在service层定义FileService接口,该接口包含上传文件到阿里云OSS的方法。例如,可以使用MultipartFile作为参数,返回上传成功后的文件URL。 2. 在controller层编写FileApiController类,该类使用@RestController注解标识为控制器,并使用@RequestMapping注解指定请求路径。在该类中,通过@Autowired注入FileService,并在文件上传的接口方法中调用FileService的上传文件方法并返回上传成功后的文件URL。 3. 在配置文件中配置阿里云OSS的相关信息,包括accessKey、secretKey、bucketName等。可以使用SpringBoot提供的@ConfigurationProperties注解来读取配置文件中的信息。 4. 在pom.xml文件中添加阿里云OSS SDK的依赖。 5. 编写上传文件的前端界面,可以使用HTML或者前端框架如Vue.js、React等。 通过以上步骤的实现,SpringBoot就可以整合阿里云OSS对象存储服务,实现文件上传和管理功能。这样可以将文件存储阿里云OSS中,提高文件的安全性和可靠性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [SpringBoot整合阿里云OSS对象存储服务的实现](https://download.csdn.net/download/weixin_38649091/12721580)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [全网最详细SpringBoot、SpringCloud整合阿里云OSS对象存储服务](https://blog.csdn.net/weixin_55076626/article/details/127924003)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值