springboot 整合阿里云oss
前言
在項目開發中,很多涉及到需要存儲文件的業務點,由于不想自建文件服務器,因此考慮使用阿里云的oss對象存儲;
OSS對象存儲簡介
對象存儲服務(Object Storage Service,簡稱OSS)
阿里云對象存儲OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存儲服務,提供99.9999999999%(12個9)的數據持久性,99.995%的數據可用性。多種存儲類型供選擇,全面優化存儲成本。非常適合存儲非結構化數據,例如視頻、圖形、日志、文本文件以及各種App應用、多終端同步軟件、網盤下載站的文件等,單個文件的大小從1字節到48.8TB,可以存儲的個數無限制;
前置操作
1、注冊阿里云賬戶并登錄至控制臺
可以直接使用自己的支付寶賬戶登錄,或者使用手機號注冊;
2、登錄控制臺,開通對象存儲OSS服務
?
3、創建bucket
使用OSS存儲的基本邏輯單元是bucket,bucket即為桶的意思,創建一個桶,即創建了一個可以上傳文件的容器,然后客戶端就可以將文件上傳到這個桶中;
點擊工作臺,創建bucket,如下我這里創建了一個名叫 "zcy1"的桶
然后可以基于這個bucket創建文件目錄,并可以測試在線上傳文件
點擊右上角個人頭像設置那里進入到 AccessKey管理
?這個里面維護著個人創建的AccessKey信息,客戶端程序中,需要使用這個AccessKey信息進行連接,并上傳文件到OSS的bucket;
?
代碼集成步驟
1、導入官方提供的SDK
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>2.8.3</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.0</version></dependency>2、配置文件中配置控制臺的AccessKey
#服務端口 server.port=8002 #服務名 spring.application.name=service-oss#阿里云 OSS #不同的服務器,地址不同 aliyun.oss.file.endpoint=管控臺的endpoint地址 aliyun.oss.file.keyid=accessKeyId aliyun.oss.file.keysecret=secretKey #bucket可以在控制臺創建,也可以使用java代碼創建 aliyun.oss.file.bucketname=你創建的bucket名稱3、添加一個配置類,讀取配置文件信息
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;@Component @ConfigurationProperties(prefix = "oss.aliyun") public class ConstantPropertiesUtils 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;ACCESS_KEY_SECRET = keySecret;BUCKET_NAME = bucketName;} }4、編寫一個測試接口,用于文件上傳
import com.congge.service.OssService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile;@RestController public class OssController {@Autowiredprivate OssService ossService;//上傳頭像,返回圖片的url給@PostMappingpublic String uploadOssFile(MultipartFile file) throws Exception{//獲取上傳文件 MultipartFile//返回圖片在oss上的路徑String url = ossService.uploadFileAvatar(file);return url;}}5、業務實現類
import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClient; import com.congge.configs.ConstantPropertiesUtils; import com.congge.service.OssService; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile;import java.io.InputStream; import java.util.UUID;@Service public class OssServiceImpl implements OssService {@Overridepublic String uploadFileAvatar(MultipartFile file) throws Exception{String endpoint = ConstantPropertiesUtils.END_POINT;String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;String bucketName = ConstantPropertiesUtils.BUCKET_NAME;String url = null;//創建OSSClient實例。OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);//獲取上傳文件輸入流InputStream inputStream = file.getInputStream();//獲取文件名稱String fileName = file.getOriginalFilename();//保證文件名唯一,去掉uuid中的'-'String uuid = UUID.randomUUID().toString().replaceAll("-", "");fileName = uuid + fileName;//把文件按日期分類,構建日期路徑:avatar/2019/02/26/文件名//String datePath = new Date().toString("yyyy/MM/dd");String datePath = "2021";//拼接fileName = datePath + "/" + fileName;//調用oss方法上傳到阿里云//第一個參數:Bucket名稱//第二個參數:上傳到oss文件路徑和文件名稱//第三個參數:上傳文件輸入流ossClient.putObject(bucketName, fileName, inputStream);//把上傳后把文件url返回//https://xppll.oss-cn-beijing.aliyuncs.com/01.jpgurl = "https://" + bucketName + "." + endpoint + "/" + fileName;//關閉OSSClientossClient.shutdown();return url;} }6、主啟動類
@SpringBootApplication public class VersionApp {public static void main(String[] args) {SpringApplication.run(VersionApp.class,args);}}運行上面的程序,調用上傳接口,使用swagger調用即可,
?
上傳成功后,接口會返回一串上傳成功的url,
?
這時候我們再返回到控制臺中查看下我們上傳的這種圖片
通過上面的步驟,我們就基于springboot與oss的整合?,完成了上傳文件到OSS的需求
?
總結
以上是生活随笔為你收集整理的springboot 整合阿里云oss的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TensorFlow 2.10 Unab
- 下一篇: Hulu九月热招 | 全职社招