目录
一、什么是阿里云OSS
二、 点击免费试用
2.1 选择第一个,点击免费试用 编辑
2.2 登录管理控制台
2.3 进入Bucket
2.4、在阿里云网站上的个人中心配置Accesskey,查询accessKeyId和accessKeySecret。
2.5、进入AccssKey管理页面应该会出现下图提示,accessKeySecret为空,不用点击下载,直接确定即可
2.6 点击创建AccessKe
编辑 三、OSS结合代码使用
3.1 引入pom.xml 依赖
3.2、创建对应的工具类AliOssUtil类,此代码是固定代码,直接CV即可
3.3 、在该工具类中有四个属性,通过上面的步骤已经获得了我们上传图片到OSS所需要的四个参数:bucketName、endpoint、accessKeyId、accessKeySecret。这些属性需要我们手动在application.properties中配置。
3.4 配置相应的model类
3.5、将工具类配置到ioc容器中,便于后续的使用。
3.6 测试:创建控制层
一、什么是阿里云OSS
阿里云对象存储服务(Object Storage Service,简称OSS)是阿里云推出的一种在线存储服务,旨在为企业和个人用户提供安全、可靠、便捷且经济高效的云存储解决方案
二、开通服务(新用户使用三个月)
https://www.aliyun.com/product/oss?spm=5176.8465980.unusable.ddetail.7df51450v1aNb1
二、 点击免费试用
2.1 选择第一个,点击免费试用
2.2 登录管理控制台
https://oss.console.aliyun.com/overview
在左侧导航栏,单击Bucket列表,然后单击创建Bucket
2.3 进入Bucket
点击概要
2.4、在阿里云网站上的个人中心配置Accesskey,查询accessKeyId和accessKeySecret。
、
2.5、进入AccssKey管理页面应该会出现下图提示,accessKeySecret为空,不用点击下载,直接确定即可
2.6 点击创建AccessKe
-
通过安全验证后可以看到生成的accessKeyId和accessKeySecret,大家下载csv文件或者复制下来,因为点击确定后不再显示accessKeySecret!!
点击确定
三、OSS结合代码使用
3.1 引入pom.xml 依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.10.2</version></dependency>
3.2、创建对应的工具类AliOssUtil类,此代码是固定代码,直接CV即可
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.io.ByteArrayInputStream;@Data
@AllArgsConstructor
//固定代码,CV直接使用
public class AliOssUtil {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;/*** 文件上传** @param bytes :传入的文件要转为byte[]* @param objectName :表示在oss中存储的文件名字。* @return*/public String upload(byte[] bytes, String objectName) {// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {// 创建PutObject请求。ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(bytes));} 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();}}//文件访问路径规则 https://BucketName.Endpoint/ObjectNameStringBuilder stringBuilder = new StringBuilder("https://");stringBuilder.append(bucketName).append(".").append(endpoint).append("/").append(objectName);return stringBuilder.toString();}
}
3.3 、在该工具类中有四个属性,通过上面的步骤已经获得了我们上传图片到OSS所需要的四个参数:bucketName、endpoint、accessKeyId、accessKeySecret。这些属性需要我们手动在application.properties中配置。
# 配置阿里云OSS(application.properties)
aliyun.oss.bucketName = hyc-8147
aliyun.oss.endpoint = oss-cn-beijing.aliyuncs.com
aliyun.oss.accessKeyId = LTAI5tE2krid8AXzidDUpn9n
aliyun.oss.accessKeySecret = 2A0Vrvj982nfRPWDVt3lp# yml版(application.yml)
#aliyun:
# oss:
# bucketName: hyc-8147
# endpoint: oss-cn-beijing.aliyuncs.com
# accessKeyId: LTAI5tE2krid8AXzidDUpn9n
# accessKeySecret: 2A0Vrvj982nfRPWDVt3lp
3.4 配置相应的model类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;/*** <p>Project: wms-project - OSSConfig</p>* <p>Powered by fpl1116 On 2024-02-28 23:26:54</p>* <p>描述:<p>** @author penglei* @version 1.0* @since 1.8*/@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
@Data
public class AliOssProperties {private String endpoint;private String accessKeyId;private String accessKeySecret;private String bucketName;}
3.5、将工具类配置到ioc容器中,便于后续的使用。
import com.fpl.model.AliOssProperties;
import com.fpl.utils.AliOssUtil;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class OssConfiguration {@Bean@ConditionalOnMissingBeanpublic AliOssUtil getAliOssUtil(AliOssProperties aliOssProperties) {
// log.info("创建OssUtil");AliOssUtil aliOssUtil = new AliOssUtil(aliOssProperties.getEndpoint(),aliOssProperties.getAccessKeyId(),aliOssProperties.getAccessKeySecret(),aliOssProperties.getBucketName());return aliOssUtil;}
}
3.6 测试:创建控制层
@RestController
@RequestMapping("api/upload")
public class UploadController {@Autowiredprivate AliOssUtil aliOssUtil;@PostMapping("/oss1")//万能上传public String upload1(@RequestBody UploadInfo uploadInfo){String name = uploadInfo.getName();String base64 = uploadInfo.getBase64();String[] base64Array = StrUtil.splitToArray(base64, "base64,");byte[] bytes = Base64.decode(base64Array[1]);name = PinyinUtil.getPinyin(name, "");String newName = System.currentTimeMillis() +"_"+name;return aliOssUtil.upload1(bytes, newName);}
}