https://www.aliyun.com/
(1)进入控制台
(2)创建根目录
创建bucket
(3)上传文件
1、如何对接
自行创建好springBoot工程并引入相关启动类
<!-- 阿里云oss依赖 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<!-- 日期工具栏依赖 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
#服务端口
server.port=8002
#服务名
spring.application.name=service-oss
#环境设置:dev、test、prod
spring.profiles.active=dev
#写自己的服务器地址参考如下
aliyun.oss.file.endpoint=oss-cn-shenzhen.aliyuncs.com
aliyun.oss.file.keyid=写自己的
aliyun.oss.file.keysecret=写自己的
#bucket可以在控制台创建,也可以使用java代码创建
#写自己的创建的文件路径 参考如下
aliyun.oss.file.bucketname=guli-da-file2022
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* @description: TODO
* @author MIS
* @date 2022/8/3 17:15
* @version 1.0
*/
@Component
public class ConstantPropertiesUtil 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;
}
}
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.baseservice.handler.GuliException;
import com.atguigu.ossservice.service.FileService;
import com.atguigu.ossservice.utils.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;
/**
* @description: TODO
* @author MIS
* @date 2022/8/3 14:28
* @version 1.0
*/
@Service
public class FileServiceImp implements FileService {
@Override
public String uploadFileOss(MultipartFile file) {
// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
String endpoint = ConstantPropertiesUtil.END_POINT;
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtil.BUCKET_NAME;
String fileName = file.getOriginalFilename();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
try {
//上传文件流
InputStream inputStream = file.getInputStream();
//优化文件名不重复
fileName= UUID.randomUUID().toString()+fileName;
//优化文件存储路径//优化文件存储路径(/2022/08/03/uuid+01.jpg)
String path=new DateTime().toString("yyyy/MM/dd");
fileName=path+"/"+fileName;
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//https://guli-file201021.oss-cn-beijing.aliyuncs.com/01.jpg
String url ="https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
} catch (IOException e) {
e.printStackTrace();
throw new GuliException(20001,"上传失败");
}
}
}
/**
* @author MIS
* @version 1.0
* @description: TODO
* @date 2022/8/3 14:29
*/
@Api(description = "文件管理")
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class FileController {
@Autowired
FileService fileService;
@ApiOperation(value = "文件上传")
@PostMapping("/uploadFile")
public R uploadFile(MultipartFile file) {
String url = fileService.uploadFileOss(file);
return R.ok().data("url", url);
}
}