栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

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

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

解决方案

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

    
        
            
                com.alibaba.cloud
                aliyun-oss-spring-boot-starter
                1.0.0
                pom
                import
            
        
    

启动报错:
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;

Oss endpoint can’t be empty.

启动配置正确写法

alibaba:
  cloud:
    access-key: 8YmeeBJBg
    secret-key: IFJn2P8GylI
    oss:
      endpoint: oss-cn-shenzhen.aliyuncs.com

注意图片配置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;

    
    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;
    }
}

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
  @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;
   @Override
    public Map policy(){



        // 设置上传到OSS文件的前缀,可置空此项。置空后,文件将上传至Bucket的根目录下。
        String dir = "test/";

        OSSClient client = new OSSClient(endpoint, accessId, accessKey);
        Map respMap = new LinkedHashMap();
        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;
    }
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/931013.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号