SpringBoot整合阿里云OSS上传文件

  1. 在pom.xml中添加OSS依赖

        <dependency>
            <groupId>com.aliyun.ossgroupId>
            <artifactId>aliyun-sdk-ossartifactId>
            <version>3.9.1version>
        dependency>
  1. 编写在application.yml中编写oss配置信息
aliyun:
  oss:
    endpoint: oss-cn-shanghai.aliyuncs.com
    accessKeyId: LTA
    secret: EyYAjI
    bucket: yygh-oos
  1. 编写oss接口文件 OssService.java
package cn.sh.ideal.service;

import org.springframework.web.multipart.MultipartFile;

/**
 * @author Steven
 * @version 1.0
 * @date 2023/1/28 15:46
 * @description
 */
public interface OssService {

    String uploadFile(MultipartFile multipartFile);
}

  1. 编写oss实现类OssServiceImpl.java
package cn.sh.ideal.service.impl;

/**
 * @author Steven
 * @version 1.0
 * @date 2023/1/28 15:47
 * @description
 */

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

import cn.sh.ideal.config.OSSProperties;
import cn.sh.ideal.service.OssService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.InitiateMultipartUploadRequest;
import com.aliyun.oss.model.InitiateMultipartUploadResult;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@RequiredArgsConstructor
@Service
@Slf4j
public class OssServiceImpl implements OssService {

    private final OSSProperties ossProperties;

    @SneakyThrows
    @Override
    public String uploadFile(MultipartFile multipartFile) {
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getSecret());
        String fileName = multipartFile.getOriginalFilename();
        InputStream inputStream = multipartFile.getInputStream();
        //生成唯一随机值
        String uuid= UUID.randomUUID().toString().replace("-","");
        fileName=uuid+fileName;
        String timeUrl=new DateTime().toString("yyyy/MM/dd");
        fileName=timeUrl+"/"+fileName;
        ossClient.putObject(ossProperties.getBucket(),fileName, inputStream);
        // 关闭oss
        ossClient.shutdown();
        String url = "https://" +
                ossProperties.getBucket()+ "."+
                ossProperties.getEndpoint() + "/"+fileName;
        return url;
    }

    public void upload(MultipartFile file){
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKeyId(), ossProperties.getSecret());
        String uuid= UUID.randomUUID().toString().replace("-","");
        String fileName = file.getOriginalFilename();
        fileName=uuid+fileName;
        String timeUrl=new DateTime().toString("yyyy/MM/dd");
        fileName=timeUrl+"/"+fileName;
        InitiateMultipartUploadRequest request = new InitiateMultipartUploadRequest(ossProperties.getBucket(), fileName);
        //初始化分片
        InitiateMultipartUploadResult initiateMultipartUploadResult = ossClient.initiateMultipartUpload(request);

    }
}

  1. 编写oss控制类OssController.java
package cn.sh.ideal.controller;

import cn.sh.ideal.result.ServerResponseEntity;
import cn.sh.ideal.service.OssService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author Steven
 * @version 1.0
 * @date 2023/1/28 15:40
 * @description
 */
@Slf4j
@RestController
@RequestMapping("/oss")
@RequiredArgsConstructor
public class OssController {

    private final OssService ossService;

    @PostMapping("/upload")
    public ServerResponseEntity<String> uploadFile(MultipartFile file){
        String url = ossService.uploadFile(file);
        return ServerResponseEntity.success(url);
    }

}

你可能感兴趣的:(Spring,Boot,文件存储,spring,boot,OSS对象存储)