• 阿里云OSS存储整合若依框架,SpringBoot


    阿里云OSS文档
    阿里云服务文档

    Cannot resolve com.alibaba.cloud:aliyun-oss-spring-boot-starter:unknown
    参考博客

    解决方案

            <!-- SpringCloud Ailibaba OSS -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>aliyun-oss-spring-boot-starter</artifactId>
            </dependency>
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>aliyun-oss-spring-boot-starter</artifactId>
                    <version>1.0.0</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    启动报错:
    Description:
    Field ossClient in com.rymall.file.service.OssSysFileServiceImpl required a bean of type ‘com.aliyun.oss.OSSClient’ that could not be found.
    The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)
    Action:
    Consider defining a bean of type ‘com.aliyun.oss.OSSClient’ in your configuration.
    Process finished with exit code 1

    参考网址
    github OSS中的ISSUE

    解决方案:

      @Autowired
    private OSSClient ossClient;
    应该改为
    
    	@Autowired
    	private OSS ossClient;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Oss endpoint can’t be empty.

    启动配置正确写法

    alibaba:
      cloud:
        access-key: 8YmeeBJBg
        secret-key: IFJn2P8GylI
        oss:
          endpoint: oss-cn-shenzhen.aliyuncs.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    注意图片配置alibaba是父节点

    在这里插入图片描述

    文件过服务器,再转阿里OSS

    package com.rymall.file.service;
    
    import com.aliyun.oss.*;
    import com.aliyun.oss.model.Callback;
    import com.rymall.file.config.MinioConfig;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Primary;
    import org.springframework.stereotype.Service;
    import org.springframework.web.multipart.MultipartFile;
    
    import java.io.ByteArrayInputStream;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    import static com.rymall.file.utils.FileUploadUtils.extractFilename;
    
    
    @Primary
    @Service
    public class OssSysFileServiceImpl implements ISysFileService {
    
        @Autowired
        private OSS ossClient;
    
        /**
         * 本地文件上传接口
         *
         * @param file 上传的文件
         * @return 访问地址
         * @throws Exception
         */
        public String uploadFile(MultipartFile file) throws Exception {
    
            try {
    //            将MultipartFile转为文件流
                InputStream inputStream = file.getInputStream();
                // 创建PutObject请求。
                //extractFilename 若依框架生成文件名字
                ossClient.putObject("rymall-product", extractFilename(file), 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());
            } finally {
                if (ossClient != null) {
                    ossClient.shutdown();
                }
            }
            return null;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    在这里插入图片描述

    alibaba:
      cloud:
        access-key: LTAI5tLbJvNXyWs4gEMxw5XE
        secret-key: g5D1D0FJT6zqAECy9nRSZnxNGTk48k
        oss:
          endpoint: oss-cn-shenzhen.aliyuncs.com
          bucket: rymall-product
          host: https://rymall-product.oss-cn-shenzhen.aliyuncs.com
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
      @Autowired
        private OSS ossClient;
    
    
        @Value("${alibaba.cloud.oss.bucket}")
        private String bucket;
    
        @Value("${alibaba.cloud.oss.endpoint}")
        private String endpoint;
    
    
        @Value("${alibaba.cloud.oss.host}")
        private String host;
    
        @Value("${alibaba.cloud.access-key}")
        private String accessId;
    
        @Value("${alibaba.cloud.secret-key}")
        private String accessKey;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
       @Override
        public Map<String, String> policy(){
    
    
    
            // 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。
            String dir = "test/";
    
            OSSClient client = new OSSClient(endpoint, accessId, accessKey);
            Map<String, String> respMap = new LinkedHashMap<String, String>();
            try {
    //            过期时间秒
                long expireTime = 3000;
                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("utf-8");
                String encodedPolicy = BinaryUtil.toBase64String(binaryData);
                String postSignature = client.calculatePostSignature(postPolicy);
                respMap.put("accessid", accessId);
                respMap.put("policy", encodedPolicy);
                respMap.put("signature", postSignature);
                respMap.put("dir", dir);
                respMap.put("host", host);
                respMap.put("expire", String.valueOf(expireEndTime / 1000));
                return respMap;
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
            return respMap;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    C++ 之 C++11新特性
    [c语言]二维数组动态分配内存
    【大数据】Hadoop三大核心组件入门
    设计模式—创建型模式之原型模式
    SparkCore系列-5、RDD 函数练习
    Mac book pro 睡眠唤醒之后,外接显示器再也无法点亮,只能重启,怎么解决?
    【云原生|Docker】Docker镜像操作
    推荐系统-排序层-模型(一):Embedding + MLP(多层感知机)模型【Deep Crossing模型:经典的Embedding+MLP模型结构】
    PIL库imagedraw画图片素材——串口发送数据正常读取数据异常有数据但是数据乱的 两个问题解决
    区块链备案
  • 原文地址:https://blog.csdn.net/qq_43751489/article/details/124931119