springBoot接入阿里云oss
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                springBoot接入阿里云oss
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                maven導(dǎo)入依賴(lài)
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!-- 阿里云OSS --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.10.2</version></dependency></dependencies>定義阿里云上傳結(jié)果實(shí)體
package com.example.demo.entity;import lombok.Data;/*** 阿里云上傳結(jié)果集** @author wushuai* @create 2021-01-25*/ @Data public class AliyunOssResult {/*** code:200成功* code: 400失敗*/private int code;/*** 上傳成功的返回url*/private String url;/*** 提示信息*/private String msg; }yml設(shè)置阿里云oss參數(shù)
aliyunOss:endpoint: "http://oss-cn-shanghai.aliyuncs.com"accessKeyId: "xxxxxxx"accessKeySecret: "xxxxxxx"bucketName: "xxxxxx"urlPrefix: "http://bucketName.oss-cn-shanghai.aliyuncs.com/"yml設(shè)置上傳文件大小限制
spring:servlet:multipart:max-file-size: 20MBmax-request-size: 20MB工具類(lèi)封裝
package com.example.demo.util;import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.model.DeleteObjectsRequest; import com.aliyun.oss.model.DeleteObjectsResult; import com.aliyun.oss.model.GeneratePresignedUrlRequest; import com.example.demo.entity.AliyunOssResult; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component;import java.io.InputStream; import java.net.URL; import java.util.Date; import java.util.List;@Component public class AliyunOSSUtil {@Value("${aliyunOss.endpoint}")private String endpoint;@Value("${aliyunOss.accessKeyId}")private String accessKeyId;@Value("${aliyunOss.accessKeySecret}")private String accessKeySecret;@Value("${aliyunOss.bucketName}")private String bucketName;@Value("${aliyunOss.urlPrefix}")private String urlPrefix;/*** 上傳文件,以IO流方式** @param inputStream 輸入流* @param objectName 唯一objectName(在oss中的文件名字)*/public AliyunOssResult upload(InputStream inputStream, String objectName) {AliyunOssResult aliyunOssResult = new AliyunOssResult();try {// 創(chuàng)建OSSClient實(shí)例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 上傳內(nèi)容到指定的存儲(chǔ)空間(bucketName)并保存為指定的文件名稱(chēng)(objectName)。ossClient.putObject(bucketName, objectName, inputStream);// 關(guān)閉OSSClient。ossClient.shutdown();aliyunOssResult.setCode(200);aliyunOssResult.setUrl(urlPrefix+objectName);aliyunOssResult.setMsg("上傳成功");} catch (Exception e) {e.printStackTrace();aliyunOssResult.setCode(400);aliyunOssResult.setMsg("上傳失敗");}return aliyunOssResult;}/*** 刪除OSS中的單個(gè)文件** @param objectName 唯一objectName(在oss中的文件名字)*/public void delete(String objectName) {try {// 創(chuàng)建OSSClient實(shí)例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 刪除文件。ossClient.deleteObject(bucketName, objectName);// 關(guān)閉OSSClient。ossClient.shutdown();} catch (Exception e) {e.printStackTrace();}}/*** 批量刪除OSS中的文件** @param objectNames oss中文件名list*/public void delete(List<String> objectNames) {try {// 創(chuàng)建OSSClient實(shí)例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 批量刪除文件。DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames));List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();// 關(guān)閉OSSClient。ossClient.shutdown();} catch (Exception e) {e.printStackTrace();}}/*** 獲取文件臨時(shí)url** @param objectName oss中的文件名* @param effectiveTime 有效時(shí)間(ms)*/public String getUrl(String objectName,long effectiveTime){OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);// 設(shè)置URL過(guò)期時(shí)間Date expiration = new Date(new Date().getTime() + effectiveTime);GeneratePresignedUrlRequest generatePresignedUrlRequest ;generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, objectName);generatePresignedUrlRequest.setExpiration(expiration);URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);return url.toString();}}controller接收調(diào)用
package com.example.demo.controller;import com.example.demo.util.AliyunOSSUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;import java.io.IOException; import java.util.UUID;@RestController @RequestMapping("/file") public class FileController {@Autowiredprivate AliyunOSSUtil aliyunOSSUtil;@RequestMapping(value = "/uploadFile")public @ResponseBodyObject uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,String strPath) throws IOException {String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);String objectName = strPath+"/"+ UUID.randomUUID().toString()+"."+suffix;return aliyunOSSUtil.upload(file.getInputStream(),objectName);} }postman測(cè)試
總結(jié)
以上是生活随笔為你收集整理的springBoot接入阿里云oss的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 苹果市值破万亿,iPhone 会涨价吗?
- 下一篇: 栅栏布局
