• 阿里云OSS图片存储


            阿里云对象存储 OSS(Object Storage Service)是一款海量、安全、低成本、高可靠的云存储服务,提供最高可达 99.995 % 的服务可用性。多种存储类型供选择,全面优化存储成本。

    视频介绍

     创建bucket

    开发文档

    上传文件demo (微改)

    1. package com.beijing.gulimall.order;
    2. import com.aliyun.oss.ClientException;
    3. import com.aliyun.oss.OSS;
    4. import com.aliyun.oss.OSSClientBuilder;
    5. import com.aliyun.oss.OSSException;
    6. import com.aliyun.oss.common.auth.CredentialsProvider;
    7. import com.aliyun.oss.common.auth.DefaultCredentialProvider;
    8. import java.io.FileInputStream;
    9. import java.io.InputStream;
    10. public class uploadFile {
    11. public static void main(String[] args) throws Exception {
    12. // RAM用户的访问密钥(AccessKey ID和AccessKey Secret)。
    13. String accessKeyId = "";
    14. String accessKeySecret = "";
    15. // 使用代码嵌入的RAM用户的访问密钥配置访问凭证。
    16. CredentialsProvider credentialsProvider = new DefaultCredentialProvider(accessKeyId, accessKeySecret);
    17. // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
    18. String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    19. // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
    20. // 填写Bucket名称,例如examplebucket。
    21. String bucketName = "gulimall-hellohai";
    22. // 填写Object完整路径,例如exampledir/exampleobject.txt。Object完整路径中不能包含Bucket名称。
    23. String objectName = "13.jpg";
    24. // 创建OSSClient实例。
    25. OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
    26. try {
    27. String content = "hello";
    28. //上传文件流
    29. InputStream inputStream = new FileInputStream("D:\\1.jpg");
    30. ossClient.putObject(bucketName, objectName, inputStream);
    31. } catch (OSSException oe) {
    32. } catch (ClientException ce) {
    33. } finally {
    34. if (ossClient != null) {
    35. ossClient.shutdown();
    36. }
    37. }
    38. }
    39. }

    对OSS进行配置封装至spring 容器

    1. <dependency>
    2. <groupId>com.alibaba.cloudgroupId>
    3. <artifactId>spring-cloud-starter-alicloud-ossartifactId>
    4. dependency>

     将配置数据放在application.yml中

    1. spring:
    2. cloud:
    3. alicloud:
    4. access-key: LTAI
    5. secret-key: ISqg
    6. oss:
    7. endpoint: oss-cn-beijing.aliyuncs.com
    8. bucket: gulimall-hellohai

    对应的java代码(简写)

    1. @Resource
    2. OSSClient ossClient;
    3. ossClient.putObject(bucketName, objectName, inputStream);

    以上使用的方式都是

    项目中OSS的使用可以改进为

    这样可以节省很多资源,不需要把文件发送到controller层,直接发送给OSS就可以

     获得签名信息

    1. package com.lihailin.gulimall.thirdparty.controller;
    2. import com.aliyun.oss.OSS;
    3. import com.aliyun.oss.OSSClient;
    4. import com.aliyun.oss.common.utils.BinaryUtil;
    5. import com.aliyun.oss.model.MatchMode;
    6. import com.aliyun.oss.model.PolicyConditions;
    7. import com.lihailin.common.utils.R;
    8. import org.springframework.beans.factory.annotation.Autowired;
    9. import org.springframework.beans.factory.annotation.Value;
    10. import org.springframework.web.bind.annotation.RequestMapping;
    11. import org.springframework.web.bind.annotation.RestController;
    12. import javax.annotation.Resource;
    13. import java.text.SimpleDateFormat;
    14. import java.util.Date;
    15. import java.util.LinkedHashMap;
    16. import java.util.Map;
    17. @RestController
    18. public class ossController {
    19. @Resource
    20. OSS ossClient;
    21. @Value("${spring.cloud.alicloud.oss.endpoint}")
    22. private String endpoint;
    23. @Value("${spring.cloud.alicloud.oss.bucket}")
    24. private String bucket;
    25. @Value("${spring.cloud.alicloud.access-key}")
    26. private String accessId;
    27. @RequestMapping("/oss/policy")
    28. public R policy() {
    29. //https://gulimall-hello.oss-cn-beijing.aliyuncs.com/hahaha.jpg
    30. String host = "https://" + bucket + "." + endpoint; // host的格式为 bucketname.endpoint
    31. // callbackUrl为 上传回调服务器的URL,请将下面的IP和Port配置为您自己的真实信息。
    32. // String callbackUrl = "http://88.88.88.88:8888";
    33. String format = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
    34. String dir = format + "/"; // 用户上传文件时指定的前缀。
    35. Map respMap = null;
    36. try {
    37. long expireTime = 30;
    38. long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
    39. Date expiration = new Date(expireEndTime);
    40. PolicyConditions policyConds = new PolicyConditions();
    41. policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
    42. policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
    43. String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
    44. byte[] binaryData = postPolicy.getBytes("utf-8");
    45. String encodedPolicy = BinaryUtil.toBase64String(binaryData);
    46. String postSignature = ossClient.calculatePostSignature(postPolicy);
    47. respMap = new LinkedHashMap();
    48. respMap.put("accessid", accessId);
    49. respMap.put("policy", encodedPolicy);
    50. respMap.put("signature", postSignature);
    51. respMap.put("dir", dir);
    52. respMap.put("host", host);
    53. respMap.put("expire", String.valueOf(expireEndTime / 1000));
    54. // respMap.put("expire", formatISO8601Date(expiration));
    55. } catch (Exception e) {
    56. // Assert.fail(e.getMessage());
    57. System.out.println(e.getMessage());
    58. }
    59. return R.ok().put("data",respMap);
    60. }
    61. }

  • 相关阅读:
    Spring Boot 如何配置 Hikari 数据库连接池
    PHP多功能投票微信小程序系统源码
    人事管理系统springboot42
    C++:STL(标准模板库)
    OSPF虚拟链路以及选路
    什么是Jsoup
    在Windows中自动压缩备份文件和目录的脚本
    必背积分表
    速卖通、阿里巴巴国际站测评补单技巧分享,需要哪些技术要求
    应用内广告竞价策略如何让APP广告变现收益最大化?
  • 原文地址:https://blog.csdn.net/weixin_42383680/article/details/133488415