springboot整合阿里云oss文件服务器
一、申请Bucket
-
进入该网址对象存储oss述
-
创建Bucket
-
创建信息
-
文件设置,可以将自己上传的文件根据类型上传不同的文件夹
-
读写权限,弄公共读即可
-
跨域设置
-
域名设置,根据情况也可以添加ssl证书,一般服务器配置里ssl,会自己识别到
二、 获取AccessKey ID、AccessKey Secret
- 点击这里
- 创建AccessKey
三、 springboot整合
3.1 在application.yml 配置参数
endpoint这里,在oss管理界面问一下快捷帮助可以弹出
3.2 oss需要的pom
<!--阿里云文件服务器oss-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.8.3</version>
</dependency>
3.3 配置 oss配置类
package com.seven.server.config.oss;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
/**
* @Author: Jia Zhenqi
* @Date: 2023/2/18 15:44
* @Version 1.0
*/
@Configuration
public class OssConfig implements InitializingBean {
//读取配置文件的内容
@Value("${aliyun.oss.file.endpoint}")
private String endpoint;
@Value("${aliyun.oss.file.keyid}")
private String keyId;
@Value("${aliyun.oss.file.keysecret}")
private String keySecret;
@Value("${aliyun.oss.file.bucketname}")
private String bucketName;
//定义公共静态常量
public static String END_POINT;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() throws Exception {
END_POINT = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}
3.4 oss的controller类
package com.seven.server.controller;
import com.seven.server.service.OssService;
import com.seven.server.util.result.RespBean;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
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: Jia Zhenqi
* @Date: 2023/2/18 15:47
* @Version 1.0
*/
@Api(tags = "oss图片上传接口管理")
@RestController
@RequestMapping("/api/oss")
public class OssController {
@Autowired
private OssService ossService;
/**
* 调用oss上传文件,返回url
* @param file
* @return
* @throws Exception
*/
@PostMapping("/upload")
public RespBean uploadOssFile(MultipartFile file, String path) throws Exception{
//获取上传文件 MultipartFile
//返回图片在oss上的路径
return ossService.uploadFileAvatar(file,path);
}
}
3.5 oss的service类以及impl
service
package com.seven.server.service;
import com.seven.server.util.result.RespBean;
import org.springframework.web.multipart.MultipartFile;
/**
* @Author: Jia Zhenqi
* @Date: 2023/2/18 15:47
* @Version 1.0
*/
public interface OssService {
/**
* 调用oss上传接口
* @param file
* @param path
* @return
* @throws Exception
*/
public RespBean uploadFileAvatar(MultipartFile file, String path) throws Exception;
}
serviceimpl
package com.seven.server.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.seven.server.config.oss.OssConfig;
import com.seven.server.service.OssService;
import com.seven.server.util.result.RespBean;
import com.seven.server.util.uuid.Uuid;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.util.UUID;
/**
* @Author: Jia Zhenqi
* @Date: 2023/2/18 15:47
* @Version 1.0
*/
@Service
public class OssServiceImpl implements OssService {
/**
* 调用oss上传接口
* @param file
* @param path
* @return
* @throws Exception
*/
@Override
public RespBean uploadFileAvatar(MultipartFile file, String path) throws Exception{
String endpoint = OssConfig.END_POINT;
String accessKeyId = OssConfig.ACCESS_KEY_ID;
String accessKeySecret = OssConfig.ACCESS_KEY_SECRET;
String bucketName = OssConfig.BUCKET_NAME;
String url = null;
//创建OSSClient实例。
OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
//保证文件名唯一,去掉uuid中的'-'
String uuid = Uuid.getUUID();
fileName = uuid + fileName;
//拼接
fileName = path + "/" + fileName;
//调用oss方法上传到阿里云
//第一个参数:Bucket名称
//第二个参数:上传到oss文件路径和文件名称
//第三个参数:上传文件输入流
ossClient.putObject(bucketName, fileName, inputStream);
//把上传后把文件url返回
//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
url = "https://" + bucketName + "." + endpoint + "/" + fileName;
//关闭OSSClient
ossClient.shutdown();
return RespBean.success("图片上传成功", url);
}
}