创建一个oss桶guli-file-xiaozhao
1、引入依赖
<!--aliyunOSS--> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>3.10.2</version> </dependency>
<!--格式化时间工具用于获取本地时间 用法:String datePath = new DateTime().toString("yyyy/MM/dd");-->
<dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency>
2、创建一个工具类用于获取常量的值
@Component public class ConstantYmlUtils 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 ENDPOINT; public static String KEYID; public static String KEYSECRET; public static String BUCKETNAME; @Override public void afterPropertiesSet() throws Exception { ENDPOINT = endpoint; KEYID = keyid; KEYSECRET = keysecret; BUCKETNAME = bucketname; } }
3、写service
package com.atguigu.oss.service; public interface OssService { String uploadFileAvatar(MultipartFile file); } @Service public class OssServiceImpl implements OssService { @Override public String uploadFileAvatar(MultipartFile file) { String endpoint = ConstantYmlUtils.ENDPOINT; String keyid = ConstantYmlUtils.KEYID; String keysecret = ConstantYmlUtils.KEYSECRET; String bucketname = ConstantYmlUtils.BUCKETNAME; // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret); try { InputStream inputStream = null; try { inputStream = file.getInputStream(); } catch (IOException e) { e.printStackTrace(); } // 创建PutObjectRequest对象。 String fileName = file.getOriginalFilename(); //在文件名称中添加一个随机的唯一的一个值 String uuid = UUID.randomUUID().toString().replace("-",""); fileName = uuid + fileName; //把文件按照日期进行分类 String datePath = new DateTime().toString("yyyy/MM/dd"); //拼接 fileName = datePath +"/"+ fileName; PutObjectRequest putObjectRequest = new PutObjectRequest(bucketname, fileName, inputStream); // 设置该属性可以返回response。如果不设置,则返回的response为空。 putObjectRequest.setProcess("true"); // 创建PutObject请求。 PutObjectResult result = ossClient.putObject(putObjectRequest); // 如果上传成功,则返回200。 System.out.println(result.getResponse().getStatusCode()); String url = "https://guli-file-xiaozhao.oss-cn-beijing.aliyuncs.com/"+fileName; return url; } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } return null; } }
4、写controller
@RestController @RequestMapping("/eduoss/fileoss") @CrossOrigin @Api(value = "用于文件上传到阿里云的接口") public class OssController { @Autowired private OssService ossService; //上传头像的方法 @PostMapping("uploadOssFile") public R uploadOssFile(MultipartFile file){ //获取上传的文件 String url = ossService.uploadFileAvatar(file); return R.ok().data("url",url); } }
测试
注意:可以参考官方sdk文档 https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.32009.0.0.15e6c927P7Kpd4