阿里云OSS-对象存储
一 介绍
阿里云对象存储OSS(Object Storage Service),是一款海量、安全、低成本、高可靠的云存储服务。使用OSS,您可以通过网络随时存储和调用包括文本、图片、音频和视频等在内的各种文件。
在我们使用了阿里云OSS对象存储服务之后,我们的项目当中如果涉及到文件上传这样的业务,在前端进行文件上传并请求到服务端时,在服务器本地磁盘当中就不需要再来存储文件了
二 账号准备
注册阿里云账户(注册完成后需要实名认证)
https://account.aliyun.com/login/login.htm?oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F
开通OSS云服务
点击左侧的 “Bucket列表”,创建一个Bucket
把bucket名称和Endpoint记录下来!!!
配置AK & SK
三 创建测试工程,
1 创建springboot项目
引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
<scope>compile</scope>
</dependency>
</dependencies>
创建yml配置文件
server:
port: 8019
spring:
application:
name: oss-server
2 SpringBoot整合OSS
2.1 添加OSS依赖
<!-- OSS依赖-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
2.2 yml配置OSS信息
server:
port: 8080
# oss配置,根据自己刚刚创建的填。
aliyun:
oss:
end-point: oss-cn-beiji......
access-key-id: LTAI5tAiBD......
access-key-secret: 3ZsC3O......
bucket-name: text-os......
2.3 读取yml配置,创建OSSConfig
/**
* @description OSS配置类
* @author lixiang
*/
@ConfigurationProperties(prefix = "aliyun.oss")
@Configuration
@Data
public class OSSConfig {
private String endPoint;
private String accessKeyId;
private String accessKeySecret;
private String bucketName;
}
2.4 编写FileOssService
public interface FileOssService {
/**
* 阿里云OSS文件上传
* @param file
* @return
*/
String uploadToOss(MultipartFile file);
}
2.5 业务层FileOssServiceImpl
@Service
@Slf4j
public class FileOssServiceImpl implements FileOssService {
@Autowired
private OSSConfig ossConfig;
/**
* 阿里云OSS文件上传
*
* @param file
*/
@Override
public String uploadToOss(MultipartFile file) {
//获取相关配置
String bucketName = ossConfig.getBucketName();
String endPoint = ossConfig.getEndPoint();
String accessKeyId = ossConfig.getAccessKeyId();
String accessKeySecret = ossConfig.getAccessKeySecret();
//创建OSS对象
OSS ossClient = new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
//获取原生文件名
String originalFilename = file.getOriginalFilename();
//JDK8的日期格式
LocalDateTime time = LocalDateTime.now();
DateTimeFormatter dft = DateTimeFormatter.ofPattern("yyyy/MM/dd");
//拼装OSS上存储的路径
String folder = dft.format(time);
String fileName = generateUUID();
String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
//在OSS上bucket下的文件名
String uploadFileName = "user/" + folder + "/" + fileName + extension;
try {
PutObjectResult result = ossClient.putObject(bucketName, uploadFileName, file.getInputStream());
//拼装返回路径
if (result != null) {
return "https://"+bucketName+"."+endPoint+"/"+uploadFileName;
}
} catch (IOException e) {
log.error("文件上传失败:{}",e.getMessage());
} finally {
//OSS关闭服务,不然会造成OOM
ossClient.shutdown();
}
return null;
}
/**
* 获取随机字符串
* @return
*/
private String generateUUID() {
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 32);
}
}
2.6 编写Controller
package com.miniodemo.miniodemo.controller;
import com.miniodemo.miniodemo.service.FileOssService;
import com.miniodemo.miniodemo.service.FileService;
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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("oss")
public class OssUploadController {
@Autowired
private FileOssService fileOssService;
/**
* 文件上传接口
* @param file
* @return
*/
@PostMapping("/upload")
public String upload(@RequestPart("file") MultipartFile file){
return fileOssService.uploadToOss(file);
}
}
2.7 用ApiFox测试一下
完成!
标签:存储,String,OSS,file,org,import,oss,SpringBoot From: https://blog.csdn.net/2301_81717523/article/details/143168029