簡單說一下這個OSS干啥用的,我們知道mysql這種關系型資料庫最好不要存長文本還有二進制資料,比如圖片,檔案等,那么這些檔案和圖片放哪里呢?
本篇說的就是放到阿里云的OSS中去,然后資料庫中只存放對應的url,我們只需要拿著這個url就可以訪問到我們需要的資源;例如用戶頭像,還有需要商品的圖片等等;
1.首先到阿里云中進入到OSS中,然后去創建一個Bucket,下面這樣:

只用修改下面三個,其他的都是默認就行,注意這個bucket的名字很重要,后面在java代碼中需要配置:

2.然后進入到創建的bucket中,找到這個endpoint復制下來,后面會用到;

3.找到你阿里云自己的id和密鑰復制下來,后面會用到:

4.新建一個springboot專案,并且常用的依賴(包括使用lombok和swagger)和OSS依賴,并且在properties組態檔中配置前面前3步復制下來的東西:
<!-- 阿里云oss依賴 -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
</dependency>
<!-- 日期工具列依賴 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
</dependency>
aliyun.oss.file.bucketname=你的bucket的名字 aliyun.oss.file.endpoint=你的endpoint路徑 aliyun.oss.file.keyid=你的阿里云的id aliyun.oss.file.keysecret=你的阿里云的密鑰
5.使用一個類和上面properties檔案中的屬性系結起來,方便我們使用:
package com.protagonist.utils; import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Getter @Setter @Component @ConfigurationProperties(prefix = "aliyun.oss.file") public class ConstantPropertiesUtils{ private String endpoint; private String keyid; private String keysecret; private String bucketname; }
6.controller代碼,回傳保存在OSS中檔案的全路徑,這個Result 就是一個對前端統一的型別,也可以直接回傳HashMap
package com.protagonist.controller; import com.protagonist.responseVO.Result; import com.protagonist.responseVO.StatusCode; import com.protagonist.service.OssService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.util.HashMap; @Api("檔案上傳") @RestController @RequestMapping("/eduoss") @CrossOrigin public class OssController { @Resource private OssService ossService; //上傳頭像的方法 @PostMapping("/fileOss") @ApiOperation(value = "檔案上傳") public Result UploadOssFile(MultipartFile file) { String url = ossService.uploadFileAvatar(file); HashMap<String, String> map = new HashMap<>(); map.put("url",url); return new Result<>(true, StatusCode.OK,"上傳成功",map); } }
7.service代碼,只需要修改那個自定義例外還有狀態碼,其他的不需要改:
package com.protagonist.service; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.protagonist.responseVO.StatusCode; import com.protagonist.servicebase.exception.ProtagonistException; import com.protagonist.utils.ConstantPropertiesUtils; import lombok.extern.slf4j.Slf4j; import org.joda.time.DateTime; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.InputStream; import java.util.UUID; @Service @Slf4j public class OssService{ @Resource private ConstantPropertiesUtils constantPropertiesUtils; public String uploadFileAvatar(MultipartFile file) { //獲取oss上傳組態檔中的引數 String bucketName = constantPropertiesUtils.getBucketname(); String endpoint = constantPropertiesUtils.getEndpoint(); String keyId = constantPropertiesUtils.getKeyid(); String keySecret = constantPropertiesUtils.getKeysecret(); OSS ossClient; InputStream inputStream; try { // 創建OSSClient實體, ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret); // 上傳檔案流 inputStream = file.getInputStream(); //為了使得檔案可以重復上傳,每次上傳的時候需要將檔案名進行修改 String fileName = file.getOriginalFilename(); log.info("圖片上傳的名字為:{}",fileName); String uuid = UUID.randomUUID().toString().replaceAll("-", ""); String newFileName = uuid + fileName; //獲取當前日期,然后以日期和新的檔案名組成全路徑,使得oss中的檔案按照日期進行分類存盤 String date = new DateTime().toString("yyyy/MM/dd"); String fullFileName = date + "/" + newFileName; log.info("圖片保存在oss的全路徑為:{}",fullFileName); //第一個引數Bucket名稱 第二個引數 上傳到oss檔案路徑和檔案名稱 ossClient.putObject(bucketName, fullFileName, inputStream); // 關閉OSSClient, ossClient.shutdown(); return "https://"+bucketName+"."+ endpoint+"/"+fullFileName; } catch (Exception e) { log.error("檔案上傳失敗",e); throw new ProtagonistException(StatusCode.REMOTEERROR,"檔案上傳oss失敗"); } } }
8.測驗,圖片上傳成功,回傳OSS中保存的url,然后前端就可以直接拿到這個url做些花里胡哨的事!


我們可以看看OSS中保存的圖片;

其實這種圖片還有檔案啥的,也可以自己自己搭建一個檔案服務器丟到里面去,簡單一點的使用docker,有興趣的可以試試!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/152074.html
標籤:Java
上一篇:環境變數配置
