springBoot接入阿里云oss的實現(xiàn)步驟
maven導(dǎo)入依賴
<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é)果實體
package com.example.demo.entity;import lombok.Data;/** * 阿里云上傳結(jié)果集 * * @author wushuai * @create 2021-01-25 */@Datapublic 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: 20MB max-request-size: 20MB
工具類封裝
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;@Componentpublic 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實例。 OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 上傳內(nèi)容到指定的存儲空間(bucketName)并保存為指定的文件名稱(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中的單個文件 * * @param objectName 唯一objectName(在oss中的文件名字) */ public void delete(String objectName) { try { // 創(chuàng)建OSSClient實例。 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實例。 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(); } } /** * 獲取文件臨時url * * @param objectName oss中的文件名 * @param effectiveTime 有效時間(ms) */ public String getUrl(String objectName,long effectiveTime){ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); // 設(shè)置URL過期時間 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 { @Autowired private AliyunOSSUtil aliyunOSSUtil; @RequestMapping(value = '/uploadFile') public @ResponseBody Object 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測試
到此這篇關(guān)于springBoot接入阿里云oss的實現(xiàn)步驟的文章就介紹到這了,更多相關(guān)springBoot接入阿里云oss內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Java發(fā)送http請求的示例(get與post方法請求)2. JS繪圖Flot如何實現(xiàn)動態(tài)可刷新曲線圖3. springboot基于Redis發(fā)布訂閱集群下WebSocket的解決方案4. IDEA項目的依賴(pom.xml文件)導(dǎo)入問題及解決5. 基于android studio的layout的xml文件的創(chuàng)建方式6. CSS自定義滾動條樣式案例詳解7. 使用ProcessBuilder調(diào)用外部命令,并返回大量結(jié)果8. python使用requests庫爬取拉勾網(wǎng)招聘信息的實現(xiàn)9. HTML實現(xiàn)title 屬性換行小技巧10. python利用后綴表達(dá)式實現(xiàn)計算器功能
