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** 15** @param file 16*/@ApiOperation(value = "https://www.shwenmu.com/wenda/文件上传")@PostMapping("upload")public R upload(@ApiParam(name = "file", value = "https://www.shwenmu.com/wenda/文件", 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;}}
推荐阅读
- 可以听网页的听书软件推荐 电脑听书软件哪个好
- 会声会影软件安装教程 会声会影破解补丁怎么安装
- 摄像头拍照软件排行榜 电脑拍照软件哪个好
- 最好的文件管理软件推荐 手机文件管理软件哪个好
- 性价比高笔记本电脑推荐 哪款笔记本性价比高
- 新手玩路亚买什么牌子好,本人一直都玩台钓现在想玩路亚有没有大师推荐一款适合新手的路
- 分享利用PS软件将彩色图片变成黑白的图文操作教程 ps软件怎么分享给别人
- 分享利用PS软件批量压缩图片的图文教程 ps软件怎么分享
- 手机上适合画画的软件排名 好用的画图软件推荐
- 做图软件添字的app推荐 图片转文字软件哪个好用