SpringBoot整合阿里云OSS上传文件

  1. 在pom.xml中添加OSS依赖
<!-- 阿里云oss依赖 -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.9.1</version>
        </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);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值