在pom.xml文件中引入依赖
<!-- 阿里云 oss 对象存储 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
在application.yml文件中配置oss相关配置
oss:
bucketName:
endPoint:
accessKeyId:
accessKeySecret:
新建Oss配置类,读取application.yml文件中的Oss相关配置
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* oss配置项
*
*/
@Component
@ConfigurationProperties(prefix = "oss")
public class OssConfig {
/**
* bucketName
*/
private String bucketName;
/**
* endPoint
*/
private String endPoint;
/**
* accessKeyId
*/
private String accessKeyId;
/**
* accessKeySecret
*/
private String accessKeySecret;
public boolean isOpen() {
return open;
}
public void setOpen(boolean open) {
this.open = open;
}
public String getBucketName() {
return bucketName;
}
public void setBucketName(String bucketName) {
this.bucketName = bucketName;
}
public String getEndPoint() {
return endPoint;
}
public void setEndPoint(String endPoint) {
this.endPoint = endPoint;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
}
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectRequest;
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 javax.annotation.Resource;
import java.net.URL;
import java.security.SecureRandom;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@RequestMapping("/oss")
public class OssController {
@Resource
private OssConfig ossConfig;
/**
* 上传图片(本地)
*
* @param file
* @return
*/
@PostMapping("/import_picture")
public String importPicture(@RequestParam("file") MultipartFile file) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String fileName = file.getOriginalFilename();
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 生成上传文件名
String finalFileName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
String objectName = sdf.format(new Date()) + "/" + finalFileName;
OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), objectName, file.getInputStream());
ossClient.putObject(putObjectRequest);
// 设置URL过期时间为1小时。
//Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
Date expiration = new Date(System.currentTimeMillis() + 10000);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(ossConfig.getBucketName(), objectName, expiration);
ossClient.shutdown();
String[] split = url.toString().split("\\?");
return split[0];
}
}
接口调用,成功返回文件路径