发布时间:2025-12-09 16:22:57 浏览次数:8
解读上图
https://www.aliyun.com/product/oss
阿里云对象存储 OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,提供数据高可用性, 多种存储类型供选择,全面优化存储成本
1.阿里云对象存储-普通上传示意图
2.分析
上传慢:用户数据需先上传到应用服务器,之后再上传到OSS。网络传输时间比直传到OSS多一倍。如果用户数据不通过应用服务器中转,而是直传到OSS,速度将大大提升。
扩展性差:如果后续用户多了,应用服务器会成为瓶颈。
费用高:需要准备多台应用服务器。由于OSS上传流量是免费的,如果数据直传到OSS,
不通过应用服务器,那么将能省下几台应用服务器
1.阿里云对象存储-服务端签名后直传示意图
2.分析
1.注册一个阿里云账号,并完成认证
2.阿里云地址:https://www.aliyun.com/
3.创建阿里云对象 Bucket 创建并测试
4.创建 RAM 用户(访问控制 RAM(Resource Access Management)是阿里云提供的一项管理用户身份与资源访问权限的服务), 得到 accessKeyId 和 accessKeySecret (可以理解成是阿里云子用户的 id 和密码)
具体步骤可以参考:https://llinp.cn/articles/2022/01/02/1641105914205.html
5.将得到 endpoint , accessKeyId , accessKeySecret 填写到代码中, 并指定要上传的文
件路径, 阿里云哪个 Bucket 和上传后的文件名是什么
参考阿里云官方文档 https://help.aliyun.com/document_detail/84781.html#p-yqj-z1w-rl2
,这里我使用的是简单上传-上传文件流的方式。
1.创建一个boot项目引入依赖
<dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.5.0</version></dependency>2.编写如下一个测试类,进行测试。
@RestControllerpublic class TestController {@RequestMapping("/test")public R test() {// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。String accessKeyId = "你的accessKeyId";String accessKeySecret = "你的accessKeySecret";// 填写Bucket名称,例如examplebucket。String bucketName = "你的bucketName";// 上传文件名 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。String objectName = "12.jpg";// 填写本地文件的完整路径,例如D:\\localpath\\examplefile.txt。// 如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。String filePath = "C:\\Users\\llp\\Desktop\\solo-fetchupload-7208404724819002506-m0Dvrtu.jpg";// 创建OSSClient实例。OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);try {//文件输入流InputStream inputStream = new FileInputStream(filePath);// 创建PutObject请求。 将文件流写入到objectName文件中ossClient.putObject(bucketName, objectName, inputStream);} 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());} catch (FileNotFoundException e) {e.printStackTrace();} finally {if (ossClient != null) {ossClient.shutdown();}}return R.ok();}}可以看到文件已经上传到阿里云oss服务器上了
1.引入依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alicloud-oss</artifactId><version>2.1.0.RELEASE</version></dependency>2.编写yaml
spring:datasource:username: rootpassword: rooturl: jdbc:mysql://192.168.56.100:3306/llpliving_commodity?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghaidriver-class-name: com.mysql.jdbc.Drivercloud:alicloud:oss:endpoint: 参考创建bucket时选择的归属区域access-key: 你的keyIdsecret-key: 你的keySecretbucket-name: 你的bucketName3.编写测试类
@Resourceprivate OSSClient ossClient;@Value("${spring.cloud.alicloud.bucket-name}")private String bucketName;//上传指定的文件到bucket@RequestMapping("/test2")public R test2() throws FileNotFoundException {String filePath = "C:\\Users\\asus\\Desktop\\solo-fetchupload-7208404724819002506-m0Dvrtu.jpg";String objectName = "13.jpg";InputStream inputStream = new FileInputStream(filePath);ossClient.putObject(bucketName, objectName, inputStream);ossClient.shutdown();return R.ok();}https://help.aliyun.com/document_detail/31926.html
https://help.aliyun.com/document_detail/91868.htm
1.引入依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alicloud-oss</artifactId><version>2.1.0.RELEASE</version></dependency>2.编写yaml
server:port: 7070spring:cloud:alicloud:oss:endpoint: oss-cn-chengdu.hangzhou.comaccess-key: 你的accessKeysecret-key: 你的secretKeybucket-name: 你的bucketName3.配置类,从yaml配置文件中读取配置信息
@Data@Component@ConfigurationProperties("spring.cloud.alicloud")public class OssProperties implements InitializingBean {@Value("${spring.cloud.alicloud.oss.endpoint}")private String endpoint;private String accessKey;private String secretKey;private String bucketName;public static String ENDPOINT;public static String KEY_ID;public static String KEY_SECRET;public static String BUCKET_NAME;@Overridepublic void afterPropertiesSet() throws Exception {this.ENDPOINT = endpoint;this.KEY_ID = accessKey;this.KEY_SECRET = secretKey;this.BUCKET_NAME = bucketName;this.ENDPOINT = endpoint;}}4.编写测试类
@RestControllerpublic class OssServiceController {//提示:这里的注入方式是 OSS 接口注入, 不要写成实现类了@Resourceprivate OSS ossClient;/*** 获取文件上传签名/授权* 这段代码从阿里云示例文档拷贝, 并做修改,去掉暂时不用的代码** @return*/@RequestMapping("/oss/policy")public R policy() {// 填写Host地址,格式为https://bucketname.endpoint。String host = "https://" + OssProperties.BUCKET_NAME + "." + OssProperties.ENDPOINT;// 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。// 我们可以将文件按照 年-月-日的形式分目录存放在阿里云String format = new SimpleDateFormat("yyyy/MM/dd").format(new Date());// 用户上传文件时指定的前缀。String dir = format + "/";// 创建ossClient实例。OSS ossClient = new OSSClientBuilder().build(OssProperties.ENDPOINT, OssProperties.KEY_ID, OssProperties.KEY_SECRET);Map<String, String> respMap = null;try {long expireTime = 30;long expireEndTime = System.currentTimeMillis() + expireTime * 1000;Date expiration = new Date(expireEndTime);PolicyConditions policyConds = new PolicyConditions();policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);byte[] binaryData = postPolicy.getBytes("utf-8");String encodedPolicy = BinaryUtil.toBase64String(binaryData);String postSignature = ossClient.calculatePostSignature(postPolicy);respMap = new LinkedHashMap<String, String>();respMap.put("accessId", OssProperties.KEY_ID);respMap.put("policy", encodedPolicy);respMap.put("signature", postSignature);respMap.put("dir", dir);respMap.put("host", host);respMap.put("expire", String.valueOf(expireEndTime / 1000));// respMap.put("expire", formatISO8601Date(expiration));} catch (Exception e) {// Assert.fail(e.getMessage());System.out.println(e.getMessage());}return R.ok().put("data", respMap);}}5.测试
浏览器: http://localhost:7070/oss/policy
6.前端拿到policy凭证之后,通过policy直接上传到阿里云
import http from '@/utils/httpRequest.js'export function policy() {return new Promise((resolve,reject)=>{http({//url: http.adornUrl("/oss/policy"),url: "http://localhost:7070/oss/policy",method: "get",params: http.adornParams({})}).then(({ data }) => {resolve(data);})});}7.前端拿到policy直接进行上传会存在跨域问题
8.解决阿里云跨域问题, 设置跨域
数据安全-跨域设置
9.再次上传,依然存在问题
10.再次上传,上传成功