官網位址:https://www.aliyun.com/product/oss
輸入官網位址開通賬号
建立bucket桶
建立Bucket桶
建立Bucket桶
測試上傳
測試上傳
上傳後的結果
開通視訊點播
在右上角的搜尋框搜尋視訊點播并點選
添加OSS自有的Bucket使狀态為正常
測試上傳視訊
上傳結果
代碼實作流程
1、建立maven工程
具體結構
2、編輯pom.xml檔案
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.gec.oss</groupId>
<artifactId>springboot-oss-file</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--阿裡雲-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.1</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-vod</artifactId>
<version>2.15.11</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-sdk-vod-upload</artifactId>
<version>1.4.15</version>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3、在resources下建立aplication.yml檔案
accessKeyId、regionId、accessKeySecret使用自己的
檢視自己的key和秘鑰
找到AccessKey管理
點選繼續使用AccessKey
檢視Secret
檢視的結果
spring:
servlet:
multipart:
max-file-size: -1
max-request-size: 500MB
# 阿裡配置
# 阿裡配置
al:
oss: #存儲
endpoint: oss-cn-guangzhou.aliyuncs.com # 域
accessKeyId: LTAI5t8SxoHMePy6f5Zju5Jc # 秘鑰
accessKeySecret: zuIn8f8m9S8PwqrqOHUSKH9N7vLN6r # 秘鑰
bucketName: camesawconquered # 桶名
url: https://camesawconquered.oss-cn-guangzhou.aliyuncs.com # 通路位址
vod: # 視訊
regionId: cn-shanghai
accessKeyId: LTAI5t8SxoHMePy6f5Zju5Jc
accessKeySecret: zuIn8f8m9S8PwqrqOHUSKH9N7vLN6r
4、在src/main/java下建立com.gec.oss.controller包、com.gec.oss.utils包
5、在com.gec.oss.utils包下建立VodConfig類和VodTemplate類:
VodConfig類
package com.gec.oss.utils;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
//oss存儲配置
@Configuration
@ConfigurationProperties("al.vod")
public class VodConfig {
//區域
private String regionId;
//通路id
private String accessKeyId;
//通路秘鑰
private String accessKeySecret;
public String getRegionId() {
return regionId;
}
public void setRegionId(String regionId) {
this.regionId = regionId;
}
public String getAccessKeyId() {
return accessKeyId;
}
public void setAccessKeyId(String accessKeyId) {
this.accessKeyId = accessKeyId;
}
public String getAccessKeySecret() {
return accessKeySecret;
}
public void setAccessKeySecret(String accessKeySecret) {
this.accessKeySecret = accessKeySecret;
}
}
VodTemplate類
package com.gec.oss.utils;
import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthRequest;
import com.aliyuncs.vod.model.v20170321.GetVideoPlayAuthResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.InputStream;
//視訊操作
@Component
public class VodTemplate {
@Autowired
private VodConfig vodConfig;
//初始化
public DefaultAcsClient initVodClient() {
DefaultProfile profile = DefaultProfile.getProfile(
vodConfig.getRegionId(),
vodConfig.getAccessKeyId(),
vodConfig.getAccessKeySecret());
return new DefaultAcsClient(profile);
}
//上傳視訊,傳回視訊id
public String uploadVideo(String fileName, InputStream inputStream) {
String title = fileName.substring(0, fileName.lastIndexOf("."));
UploadStreamRequest request = new UploadStreamRequest(
vodConfig.getAccessKeyId(),
vodConfig.getAccessKeySecret(),
title,
fileName,
inputStream);
UploadVideoImpl uploader = new UploadVideoImpl();
UploadStreamResponse response = uploader.uploadStream(request);
String videoId = response.getVideoId();
return videoId;
}
//擷取播放憑證函數
public GetVideoPlayAuthResponse getVideoPlayAuth(String videoId) throws Exception {
GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
request.setVideoId(videoId);
return initVodClient().getAcsResponse(request);
}
}
6、com.gec.oss.controller包下建立FileController類:
FileController類
package com.gec.oss.controller;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.gec.oss.utils.VodTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
@CrossOrigin("*") //後端跨域
public class FileController {
@Value("${al.oss.bucketName}")
private String bucketName;
@Value("${al.oss.endpoint}")
private String endpoint;
@Value("${al.oss.accessKeyId}")
private String accessKeyId;
@Value("${al.oss.accessKeySecret}")
private String accessKeySecret;
@Value("${al.oss.url}")
private String url;
@Autowired
private VodTemplate vodTemplate;
//傳回圖檔的通路位址
@PostMapping("/uploadPicFile")
public String uploadPicFile(MultipartFile file){
//建立ossclient執行個體
OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
if (!ossClient.doesBucketExist(bucketName)){
System.out.println("你的Bucket不存在");
ossClient.createBucket(bucketName);
}else {
System.out.println("已經建立了bucket");
}
//設計檔案存儲對象(路徑+檔案名)
SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMdd");
String filename=file.getOriginalFilename();
String objectName=sdf.format(new Date())+"/"+filename;
try{
//存儲對象
ossClient.putObject(bucketName,objectName,file.getInputStream());
}catch (IOException e){
e.printStackTrace();
}
//
String httpUrl=url+"/"+objectName;
System.out.println(httpUrl);
return httpUrl;
}
@RequestMapping("/uploadVideo")
@CrossOrigin
public String uploadVideo(MultipartFile uploadVideo) throws IOException{
String videoId=this.vodTemplate.uploadVideo(uploadVideo.getOriginalFilename(),uploadVideo.getInputStream());
System.out.println(videoId);
return videoId;
}
}
7、在com.gec.oss下建立FileApp啟動類
FileApp
package com.gec.oss;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FileApp {
public static void main(String[] args) {
SpringApplication.run(FileApp.class,args);
}
}
8、運作FileApp類
FileApp運作結果
9、打開Postman并進行操作
上傳檔案流程
點選Send人,然後去OSS管理控制台看有沒有添加檔案
OSS管理控制台上傳的結果
上傳視訊流程
然後去視訊點播控制台看一下剛上傳的視訊
視訊點播控制台上傳的結果