https://oss.sonatype.org/service/local/staging/deploy/maven2/</url> <releases> <enabled>true</enabled> </releases> <snapshots> <enabled>true</enabled> </snapshots> </repository> </repositories>
1.2,添加Jar包依赖 <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-JAVA-sdk-core</artifactId> <version>4.5.1</version> </dependency> <dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-vod</artifactId> <version>2.15.11</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.62</version> </dependency>
3.初始化
根据文档示例创建 AliyunVODSDKUtils.java
public class OSSTest {// Endpoint以杭州为例,其它Region请按实际情况填写 。String endpoint = "oss-cn-beijing.aliyuncs.com";// 阿里云主账号AccessKey拥有所有API的访问权限,风险很高 。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号 。String accessKeyId = "LTAI5tSjEbLtaqn8HMe4zF4G";String accessKeySecret = "mGgEO1ueh5WdVK4oK4kJOSPQgHPR5m";String bucketName = "fire-file";@Testpublic void testCreateBucket() {// 创建OSSClient实例 。OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);// 创建存储空间 。ossClient.createBucket(bucketName);// 关闭OSSClient 。System.out.println(ossClient.listBuckets());//ossClient.shutdown();}@Testpublic void testExist() {// 创建OSSClient实例 。OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);boolean exists = ossClient.doesBucketExist(bucketName);System.out.println(exists);// 关闭OSSClient 。ossClient.shutdown();}}
4.springboot集成
FileUploadControllerpackage com.atguigu.controller;import com.atguigu.commonutils.R;import com.atguigu.service.FileService;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import org.springframework.web.multipart.MultipartFile;@RestController@RequestMapping("/eduoss")@CrossOriginpublic class FileUploadController {@Autowiredprivate FileService fileService;/*** 13* 文件上传* 14* <p>* 15** @param file 16*/@ApiOperation(value = https://www.isolves.com/it/cxkf/kj/2021-06-23/"文件上传")@PostMapping("upload")public R upload(@ApiParam(name = "file", value = "文件", required = true) @RequestParam("file") MultipartFile file) {String uploadUrl = fileService.upload(file);//返回r对象return R.ok().message("文件上传成功").data("url", uploadUrl);}}servicepackage com.atguigu.service.impl;import com.aliyun.oss.OSS;import com.aliyun.oss.OSSClientBuilder;import com.atguigu.service.FileService;import com.atguigu.utils.ConstantPropertiesUtil;import org.joda.time.DateTime;import org.springframework.stereotype.Service;import org.springframework.web.multipart.MultipartFile;import java.io.InputStream;import java.util.UUID;@Servicepublic class FileServiceImpl implements FileService {@Overridepublic String upload(MultipartFile file) {// 工具类获取值String endpoint = ConstantPropertiesUtil.END_POINT;String accessKeyId = ConstantPropertiesUtil.ACCESS_KEY_ID;String accessKeySecret = ConstantPropertiesUtil.ACCESS_KEY_SECRET;String bucketName = ConstantPropertiesUtil.BUCKET_NAME;try {// 创建OSS实例 。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);//获取上传文件输入流InputStream inputStream = file.getInputStream();//获取文件名称String fileName = file.getOriginalFilename();//1 在文件名称里面添加随机唯一的值String uuid = UUID.randomUUID().toString().replaceAll("-","");// yuy76t5rew01.jpgfileName = uuid+fileName;//2 把文件按照日期进行分类//获取当前日期//2019/11/12String datePath = new DateTime().toString("yyyy/MM/dd");//拼接//2019/11/12/ewtqr313401.jpgfileName = datePath+"/"+fileName;//调用oss方法实现上传//第一个参数Bucket名称//第二个参数上传到oss文件路径和文件名称aa/bb/1.jpg//第三个参数上传文件输入流ossClient.putObject(bucketName,fileName , inputStream);// 关闭OSSClient 。ossClient.shutdown();//把上传之后文件路径返回//需要把上传到阿里云oss路径手动拼接出来//https://edu-guli-1010.oss-cn-beijing.aliyuncs.com/01.jpgString url = "http://"+bucketName+"."+endpoint+"/"+fileName;return url;}catch(Exception e) {e.printStackTrace();return null;}}}utilspackage com.atguigu.utils;import org.springframework.beans.factory.InitializingBean;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;@Componentpublic 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;@Overridepublic void afterPropertiesSet() throws Exception {END_POINT = endpoint;ACCESS_KEY_ID = keyId;System.out.println(ACCESS_KEY_ID+"==========================");ACCESS_KEY_SECRET = keySecret;BUCKET_NAME = bucketName;}}
推荐阅读
- Beyond the Cache——阿里云Tair云内存数据库场景解读
- SpringBoot整合定时器:定时任务不再硬编码,动态定时刷起来
- SpringBoot的可视化接口开发工具
- Ubuntu 20.04更换阿里云源及安装完MySQL修改密码
- SpringBoot:拒绝大文件,夏天到了,来给jar包瘦个身
- 硬核资源!阿里内部强推IntelliJ IDEA 中的常用配置,还不赶紧收藏
- Springboot 动态设置注解参数值
- 聊一聊 SpringBoot 中配置加载优先级?
- springboot三种拦截器
- 2021年校招程序员之阿里四个部门的十轮面试问题