一、簡介
阿里雲對象存儲服務(Object Storage Service,簡稱 OSS),是阿里雲提供的海量、安全、低成本、高可靠的雲存儲服務。其數據設計持久性不低於 99.9999999999%(12 個 9),服務設計可用性(或業務連續性)不低於 99.995%。
OSS 具有與平臺無關的 RESTful API 接口,您可以在任何應用、任何時間、任何地點存儲和訪問任意類型的數據。
您可以使用阿里雲提供的 API、SDK 接口或者 OSS 遷移工具輕鬆地將海量數據移入或移出阿里雲 OSS。數據存儲到阿里雲 OSS 以後,您可以選擇標準存儲(Standard)作爲移動應用、大型網站、圖片分享或熱點音視頻的主要存儲方式,也可以選擇成本更低、存儲期限更長的低頻訪問存儲(Infrequent Access)和歸檔存儲(Archive)作爲不經常訪問數據的存儲方式。
二、基本使用
創建bucket,獲取AccessKey ID和AccessKey Secret。
以上是關於阿里雲oss的簡單介紹,如想進一步瞭解,可自行進行百度。
三、代碼實例
1、配置文件
#服務端口
server.port=8002
#服務名
spring.application.name=service-oss
#環境設置:dev、test、prod
spring.profiles.active=dev
#阿里雲 OSS
#不同的服務器,地址不同
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.file.keyid=LTAI4GJ2dfYZAmAusqM4qwti
aliyun.oss.file.keysecret=JwctWzF9sxACGAX7FjZ5wd8ewvjCPl
#bucket可以在控制檯創建,也可以使用java代碼創建
aliyun.oss.file.bucketname=edu-csdn
2、工具類
package com.atguigu.oss.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
//當項目已啓動,spring接口,spring加載之後,執行接口一個方法
@Component
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_POIND;
public static String ACCESS_KEY_ID;
public static String ACCESS_KEY_SECRET;
public static String BUCKET_NAME;
@Override
public void afterPropertiesSet() throws Exception {
END_POIND = endpoint;
ACCESS_KEY_ID = keyId;
ACCESS_KEY_SECRET = keySecret;
BUCKET_NAME = bucketName;
}
}
3、service
接口
package com.atguigu.oss.service;
import org.springframework.web.multipart.MultipartFile;
public interface OssService {
String uploadFileAvatar(MultipartFile file);
}
實現類
package com.atguigu.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.oss.service.OssService;
import com.atguigu.oss.utils.ConstantPropertiesUtils;
import org.joda.time.DateTime;
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 {
//上傳頭像到oss
@Override
public String uploadFileAvatar(MultipartFile file) {
// 工具類獲取值
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.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.jpg
fileName = uuid+fileName;
//2 把文件按照日期進行分類
//獲取當前日期
// 2019/11/12
String datePath = new DateTime().toString("yyyy/MM/dd");
//拼接
// 2019/11/12/ewtqr313401.jpg
fileName = 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.jpg
String url = "https://"+bucketName+"."+endpoint+"/"+fileName;
return url;
}catch(Exception e) {
e.printStackTrace();
return null;
}
}
}
4、controller
package com.atguigu.oss.controller;
import com.atguigu.oss.service.OssService;
import org.springframework.beans.factory.annotation.Autowired;
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 com.atguigu.commonutils.R;
@RestController
@RequestMapping("/eduoss/fileoss")
@CrossOrigin
public class OssController {
@Autowired
private OssService ossService;
//上傳頭像的方法
@PostMapping
public R uploadOssFile(MultipartFile file) {
//獲取上傳文件 MultipartFile
//返回上傳到oss的路徑
String url = ossService.uploadFileAvatar(file);
return R.Ok().data("url",url);
}
}
5、前端vue
前端主要代碼請參考:【Spring Boot 17】Spring Boot + Vue 實現CRUD
四、實現效果
阿里雲結合springboot+vue整合CRUD的實現效果,成功上傳頭像
上一篇:【全棧最全Java框架總結】SSH、SSM、Springboot