2024/04/24(团队开发)
今天是团队开发的第五天,今天的例会上我向我的团队成员们讲述了我已经可以实现在前端提交视频到后端了,并且向他们提出了我接下来的问题就是该如何将视频和图片存储下来。我们团队中的一个队友提议我,可以将视频存储到阿里云。于是我今天的目标任务——将视频存储到阿里云。
在网上找到了阿里云的使用方法,在OSS的官方文档中也有sdk的使用示例
import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.common.auth.CredentialsProviderFactory; import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider; import java.io.ByteArrayInputStream; public class Demo { public static void main(String[] args) throws Exception { // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。 String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // 填写Bucket名称,例如examplebucket。 String bucketName = "examplebucket"; // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。 String objectName = "exampledir/exampleobject.txt"; // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); try { String content = "Hello OSS"; ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(content.getBytes())); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
使用前得新建自己的oss,然后记录id和密码将其存入环境变量,最后执行存储。
然后将其修改得到了我的工具类
package com.share.viedo_app.util; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.common.auth.CredentialsProviderFactory; import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider; import com.aliyun.oss.model.PutObjectRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import java.io.InputStream; import java.util.UUID; @Component public class AliOSSUtils { @Autowired AliOSSproperties aliOSSproperties; public String upload(MultipartFile file) throws Exception{ String endpoint=aliOSSproperties.getEndpoint(); String bucketName=aliOSSproperties.getBucketName(); // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。 EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); InputStream inputStream=file.getInputStream(); //获取原始文件名 String originalFilenmae =file.getOriginalFilename(); //构造唯一的文件名(不能重复) -uuid 通用唯一识别码 String fileName= UUID.randomUUID().toString()+originalFilenmae.substring(originalFilenmae.lastIndexOf(".")); // 创建OSSClient实例。 OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider); PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName,fileName, inputStream); ossClient.putObject(putObjectRequest); String url=endpoint.split("//")[0]+"//"+bucketName+"."+endpoint.split("//")[1]+"/"+fileName; ossClient.shutdown(); return url; } }
package com.share.viedo_app.util; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @Component @ConfigurationProperties(prefix = "aliyun.oss") public class AliOSSproperties { private String endpoint; private String bucketName; }
实体类中的endpoint和bucketName都放置在配置文件中。