学习谷粒学院对象存储 — logo上传功能

对象存储

#### 谷粒商城上传logo
  • 创建第三方服务模块
  • 修改pom文件

    4.0.0

        org.springframework.boot
        spring-boot-starter-parent
        2.6.8

    com.cui
    gulimall-thirdparty
    0.0.1-SNAPSHOT
    gulimall-thirdparty
    gulimall-thirdparty

        11

            org.springframework.boot
            spring-boot-configuration-processor
            true

            com.cui.gulimall-common
            gulimall-common
            0.0.1-SNAPSHOT

                    com.baomidou
                    mybatis-plus-boot-starter

                    org.springframework.cloud
                    spring-cloud-commons

                    org.springframework.cloud
                    spring-cloud-context

            org.springframework.boot
            spring-boot-starter-web

            org.springframework.boot
            spring-boot-starter-test
            test

            org.springframework.cloud
            spring-cloud-starter-bootstrap
            3.1.2

            org.springframework.cloud
            spring-cloud-starter-openfeign

            com.alibaba.cloud
            aliyun-oss-spring-boot-starter

                com.alibaba.cloud
                spring-cloud-alibaba-dependencies
                2021.0.1.0
                pom
                import

                org.springframework.cloud
                spring-cloud-dependencies
                2021.0.2
                pom
                import

                com.alibaba.cloud
                aliyun-spring-boot-dependencies
                1.0.0
                pom
                import

                org.springframework.boot
                spring-boot-maven-plugin

  • 修改配置文件
nacos服务地址
spring.cloud.nacos.server-addr=192.168.215.137:8848
项目名
spring.application.name=thirdparty
端口号
server.port=30000
修改路径匹配规则  解决swagger冲突问题
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

服务名
spring.application.name=thirdparty
nacos服务地址
spring.cloud.nacos.config.server-addr=192.168.215.137:8848
命名空间
spring.cloud.nacos.config.namespace=d4100c2a-0991-4f7e-8e84-2db324678a0d
配置文件id
spring.cloud.nacos.config.extension-configs[0].data-id=gulimall_thirdparty.properties
开启同步更新配置
spring.cloud.nacos.config.extension-configs[0].refresh=true
#默认分组
spring.cloud.nacos.config.extension-configs[0].group=DEFAULT_GROUP

  • 开启服务发现
  • 在nacos中配置相关属性
  • 创建命名空间
  • 创建配置文件
  • 在配置文件中添加AK等信息
alibaba.cloud.accessKey=LTAI5tKbSBpALoHC7JkBsF4g
alibaba.cloud.secretKey=AkpjpwVSqtiwQFyA66e2UAghxn6xK9
alibaba.cloud.oss.endpoint=oss-cn-beijing.aliyuncs.com
alibaba.cloud.bucket=exce-cui
  • 创建接口
package com.cui.gulimallthirdparty.controller;

import com.cui.gulimall.common.utils.Result;
import com.cui.gulimallthirdparty.service.ObjectStorageService;
import com.cui.gulimallthirdparty.service.impl.ObjectStorageServiceImpl;
import io.swagger.annotations.Api;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

/**
 * @author 崔令雨
 * @date 2022/5/31 9:09
 * @Version 1.0
 */
@RestController
@Api("上传文件控制")
@RequestMapping("thirdparty/oss")
public class ObjectStorageController {

    private final ObjectStorageService objectStorageService;

    @Autowired
    public ObjectStorageController(ObjectStorageService objectStorageService) {
        this.objectStorageService = objectStorageService;
    }

    @PostMapping("upload")
    public Result> uploadFile() {

        Map uploadToken = objectStorageService.upload();

        return new Result>().ok(uploadToken);
    }

}

  • service层
package com.cui.gulimallthirdparty.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.Credentials;
import com.aliyun.oss.common.auth.CredentialsProvider;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import com.cui.gulimallthirdparty.service.ObjectStorageService;

import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author 崔令雨
 * @date 2022/5/31 9:10
 * @Version 1.0
 */
@Service
public class ObjectStorageServiceImpl implements ObjectStorageService {

    private final OSS client;

    @Autowired
    public ObjectStorageServiceImpl(OSS client) {
        this.client = client;
    }

    @Value("${alibaba.cloud.accessKey}")
    private String accessKey;
    @Value("${alibaba.cloud.secretKey}")
    private String secretKey;
    @Value("${alibaba.cloud.bucket}")
    private String bucket;
    @Value("${alibaba.cloud.oss.endpoint}")
    private String endpoint;

    @Override
    public Map upload() {
// 填写Bucket名称,例如examplebucket。
// 填写Host地址,格式为https://bucketname.endpoint。
        String host = "https://" + bucket + "." + endpoint;
// 设置上传回调URL,即回调服务器地址,用于处理应用服务器与OSS之间的通信。OSS会在文件上传完成后,把文件上传信息通过此回调URL发送给应用服务器。
        String callbackUrl = "https://192.168.0.0:8888";
// 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。

        String dir = LocalDate.now().toString();
        Map 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 = client.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = client.calculatePostSignature(postPolicy);

            respMap = new LinkedHashMap();
            respMap.put("accessid", accessKey);
            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 respMap;
    }
}

  • 前段代码 如果不双向绑定图片不回加载

需要导入依赖才能开启bootrap.properties==


            org.springframework.cloud
            spring-cloud-starter-bootstrap
            3.1.2

使用@ConfigurationProperties注解配置元数据需要导入依赖


            org.springframework.boot
            spring-boot-configuration-processor
            true

Original: https://www.cnblogs.com/cuiwnd/p/16330608.html
Author: 01cui
Title: 学习谷粒学院对象存储 — logo上传功能

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/715255/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Flask Post 获取json数据,小文件实例

    @app.route("/get_parserResult", methods=[‘POST’]) def get_parserResult(): data =…

    技术杂谈 2023年6月1日
    088
  • Java ArrayList removeIf() 方法

    removeIf() 方法用于删除所有满足特定条件的数组元素。 removeIf() 方法的语法为: arraylist.removeIf(Predicate filter) 注:…

    技术杂谈 2023年7月24日
    061
  • idea tomcat启动后没有编译java文件

    在项目结构artifact处,Available Elements右键项目,选择 Put into Output Root,就会将依赖和classes编译到out目录 需要重启id…

    技术杂谈 2023年7月11日
    091
  • 入住感言

    希望我的博客真的可以帮助到别人,也希望自己能够坚持下去。这些年回想起来,我经常做的事就是感动自己。我并没有自己想象的这么努力,虚有其表罢了!几年后的今天希望自己不会在这么想,唯有时…

    技术杂谈 2023年7月11日
    066
  • 数据仓库之汇总层(DWS)设计概要

    合理的数据仓库分层一方面能够降低耦合性,提高重用性,可读性可维护性,另一方面也能提高运算的效率,影响到数据需求迭代的速度,近而影响到产品决策的及时性。建立数据分层可以提炼公共层,避…

    技术杂谈 2023年5月31日
    0117
  • 2022.16 数字水印2

    安全技术包含3类:信息隐藏、访问控制和密码学。 典型的安全应用示例: 信息隐藏主要可分为隐写术和水印技术等两大类。隐写术的保护对象是隐秘消息,水印技术的保护对象是载体本身。 数字水…

    技术杂谈 2023年5月30日
    090
  • PyTorch 介绍 | 使用 TORCH.AUTOGRAD 自动微分

    训练神经网络时,最常用的算法就是 反向传播。在该算法中,参数(模型权重)会根据损失函数关于对应参数的梯度进行调整。 为了计算这些梯度,PyTorch内置了名为 torch.auto…

    技术杂谈 2023年7月25日
    060
  • Golang仿云盘项目-2.3实现文件下载、修改、删除接口

    本文来自博客园,作者:Jayvee,转载请注明原文链接:https://www.cnblogs.com/cenjw/p/16462188.html 目录结构 ❯ pwd /home…

    技术杂谈 2023年7月24日
    083
  • SpringSecurity 自定义表单登录

    SpringSecurity 自定义表单登录 本篇主要讲解 在SpringSecurity中 如何 自定义表单登录 , SpringSecurity默认提供了一个表单登录,但是实际…

    技术杂谈 2023年7月11日
    070
  • [转]Confluence Jira Issues Macro(Jira Issues宏)

    截图:带有Jira Issue宏的项目状态页面,显示必须在发布前解决的Issue。 连接Confluence和Jira 在您可以使用此宏之前,您的Confluence和Jira系统…

    技术杂谈 2023年5月30日
    0131
  • docker学习:docker-centos安装

    docker search centos docker pull centos docker images docker run -itd –privileged=tr…

    技术杂谈 2023年7月10日
    071
  • 默认端口

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    技术杂谈 2023年5月31日
    0102
  • 哈希表(HashTable)

    哈希表 哈希表:也叫做散列表。是根据关键字和值(Key-Value)直接进行访问的数据结构。也就是说,它通过关键字 key 和一个映射函数 Hash(key) 计算出对应的值 va…

    技术杂谈 2023年7月23日
    0126
  • Python去水印方法(无需安装任何库)

    分享一个Python 自带库去水印的方法 今天用WPS将PDF转图片,发现没有会员就会自带水印,于是萌生了用Python去水印的想法 from itertools import p…

    技术杂谈 2023年7月24日
    088
  • 一种基于二分法的变步长批量处理算法

    1、前言 变步长批量处理算法,在实现某些功能时,非常需要。如数据传输和数据导入时,使用可变步长的批量处理算法,可以极大地提高系统的性能,。 在不稳定的网络环境下,传输失败的几率提高…

    技术杂谈 2023年6月21日
    089
  • 美团大脑百亿级知识图谱的构建及应用进展

    分享嘉宾:张鸿志博士 美团 算法专家 编辑整理:廖媛媛 美的集团 出品平台:DataFunTalk 导读:美团作为中国最大的在线本地生活服务平台,连接着数亿用户和数千万商户,其背后…

    技术杂谈 2023年7月25日
    073
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球