1.开启阿里云OSS服务
1.开启服务
登录阿里云官网
开启对象存储服务后(按流量计费),进入对象存储控制台
创建Bucket
2.配置跨域
如果是微服务项目需要配置跨域
创建规则
3.生成密钥
查看endpoint和url
点击头像, 进入Accesskey管理
创建AccessKey
至此得到信息
bucket:gulimall-oss123
endpoint:oss-cn-hangzhou.aliyuncs.com
url:gulimall-oss123.oss-cn-hangzhou.aliyuncs.com
AccessKey ID:LTAI5tS2xGi7WUz7GbaVnK2z
AccessKey Secret:l5VQHnGdmNLXXPyDTGinhWN8SSMYEU
添加阿里云依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.15.1</version>
</dependency>
配置添加
#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.endpoint=oss-cn-hangzhou.aliyuncs.com
aliyun.oss.accessKeyId=LTAI5tS2xGi7WUz7GbaVnK2z
aliyun.oss.secret=l5VQHnGdmNLXXPyDTGinhWN8SSMYEU
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.bucket=gulimall-oss123
ConstantOssPropertiesUtils
package com.atguigu.gulimall.mytest.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ConstantOssPropertiesUtils implements InitializingBean {
@Value("${aliyun.oss.endpoint}")
private String endpoint;
@Value("${aliyun.oss.accessKeyId}")
private String accessKeyId;
@Value("${aliyun.oss.secret}")
private String secret;
@Value("${aliyun.oss.bucket}")
private String bucket;
public static String EDNPOINT;
public static String ACCESS_KEY_ID;
public static String SECRECT;
public static String BUCKET;
@Override
public void afterPropertiesSet() throws Exception {
EDNPOINT=endpoint;
ACCESS_KEY_ID=accessKeyId;
SECRECT=secret;
BUCKET=bucket;
}
}
服务调用
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
/**
* oss上传service
*/
@Service
public class OSSFileService {
private static final String BUCKET_PRE = "order_import";
public String upload(MultipartFile file) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
String bucketName = ConstantOssPropertiesUtils.BUCKET;
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
InputStream inputStream = file.getInputStream();
String fileName = file.getOriginalFilename();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String timeUrl = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
fileName = BUCKET_PRE + "/" + fileName + timeUrl;
//调用方法实现上传
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//上传之后文件路径
String url = "https://" + bucketName + "." + endpoint + "/" + fileName;
//返回
return url;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
public String upload(ByteArrayOutputStream outputStream, String originalFilename) {
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = ConstantOssPropertiesUtils.EDNPOINT;
String accessKeyId = ConstantOssPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantOssPropertiesUtils.SECRECT;
String bucketName = ConstantOssPropertiesUtils.BUCKET;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传文件流。
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String timeUrl = LocalDateTime.now(ZoneOffset.of("+8")).format(formatter);
String fileName = BUCKET_PRE + "/" + timeUrl + originalFilename;
//调用方法实现上传
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//上传之后文件路径
return "https://" + bucketName + "." + endpoint + "/" + fileName;
}
}