阿里云oss-cloud-sdk-springboot3兼容问题

本文涉及的产品
对象存储 OSS,20GB 3个月
对象存储OSS,敏感数据保护2.0 200GB 1年
对象存储 OSS,恶意文件检测 1000次 1年
简介: 阿里云oss-cloud-sdk-springboot3兼容问题

前两天提到了:

minio临时凭证直传切换到阿里云oss | 阿超

但是忘记说依赖和配置了,因为我本地是jdk17+springboot3,所以需要修改,首先是依赖:

<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss -->
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
            <version>3.17.4</version>
        </dependency>

按照我原来的博客里肯定是不行的

阿里云OSS临时凭证前后端配合上传文件 | 阿超

不能使用原来的

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alicloud-oss</artifactId>
    <version>2.2.0.RELEASE</version>
</dependency>

因为不兼容了

这里我们配置的格式就不改,还是

spring:
  cloud:
    alicloud:
      access-key: ACCESSKEY
      secret-key: SECRETKEY
      oss:
        endpoint: oss-cn-chengdu.aliyuncs.com
        bucket: waibi

然后我们自己复制配置文件类:

枚举

/*
 * Copyright (C) 2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



/**
 * 阿里云授权模式
 *
 * @author xiaolongzuo
 */
public enum AliCloudAuthorizationMode {

    /**
     * AK/SK授权
     */
    AK_SK,
    /**
     * STS授权
     */
    STS

}

配置文件类

/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * @author xiaolongzuo
 */
@ConfigurationProperties("spring.cloud.alicloud")
public class AliCloudProperties {

    /**
     * alibaba cloud access key.
     */
    private String accessKey;

    /**
     * alibaba cloud secret key.
     */
    private String secretKey;

    public String getAccessKey() {
        return accessKey;
    }

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public String getSecretKey() {
        return secretKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

}

以及:

/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */



import com.aliyun.oss.ClientBuilderConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;

/**
 * {@link ConfigurationProperties} for configuring OSS.
 *
 * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
 * @author xiaolongzuo
 */
@ConfigurationProperties("spring.cloud.alicloud.oss")
public class OssProperties {

    /**
     * Authorization Mode, please see <a href=
     * "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
     * docs</a>.
     */
    @Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}")
    private AliCloudAuthorizationMode authorizationMode;

    /**
     * Endpoint, please see <a href=
     * "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
     * docs</a>.
     */
    private String endpoint;

    /**
     * Sts token, please see <a href=
     * "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
     * docs</a>.
     */
    private StsToken sts;

    /**
     * Client Configuration, please see <a href=
     * "https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.659.29f145dc3KOwTh">oss
     * docs</a>.
     */
    private ClientBuilderConfiguration config;

    public AliCloudAuthorizationMode getAuthorizationMode() {
        return authorizationMode;
    }

    public void setAuthorizationMode(AliCloudAuthorizationMode authorizationMode) {
        this.authorizationMode = authorizationMode;
    }

    public ClientBuilderConfiguration getConfig() {
        return config;
    }

    public void setConfig(ClientBuilderConfiguration config) {
        this.config = config;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public StsToken getSts() {
        return sts;
    }

    public void setSts(StsToken sts) {
        this.sts = sts;
    }

    public static class StsToken {

        private String accessKey;

        private String secretKey;

        private String securityToken;

        public String getAccessKey() {
            return accessKey;
        }

        public void setAccessKey(String accessKey) {
            this.accessKey = accessKey;
        }

        public String getSecretKey() {
            return secretKey;
        }

        public void setSecretKey(String secretKey) {
            this.secretKey = secretKey;
        }

        public String getSecurityToken() {
            return securityToken;
        }

        public void setSecurityToken(String securityToken) {
            this.securityToken = securityToken;
        }

    }

}

然后是配置类:


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

/**
 * AliCloudConfig
 *
 * @author achao@apache.org
 */
@Configuration
@EnableConfigurationProperties({AliCloudProperties.class, OssProperties.class})
public class AliCloudConfig {

    @Bean
    public OSS ossClient(AliCloudProperties aliCloudProperties,
                         OssProperties ossProperties) {
        if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.AK_SK) {
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()),
                    "Oss endpoint can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getAccessKey()),
                    "${spring.cloud.alicloud.access-key} can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getSecretKey()),
                    "${spring.cloud.alicloud.secret-key} can't be empty.");
            return new OSSClientBuilder().build(ossProperties.getEndpoint(),
                    aliCloudProperties.getAccessKey(), aliCloudProperties.getSecretKey(),
                    ossProperties.getConfig());
        } else if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.STS) {
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()),
                    "Oss endpoint can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getAccessKey()),
                    "Access key can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecretKey()),
                    "Secret key can't be empty.");
            Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecurityToken()),
                    "Security Token can't be empty.");
            return new OSSClientBuilder().build(ossProperties.getEndpoint(),
                    ossProperties.getSts().getAccessKey(),
                    ossProperties.getSts().getSecretKey(),
                    ossProperties.getSts().getSecurityToken(), ossProperties.getConfig());
        } else {
            throw new IllegalArgumentException("Unknown auth mode.");
        }
    }
}

即可使用

相关实践学习
通义万相文本绘图与人像美化
本解决方案展示了如何利用自研的通义万相AIGC技术在Web服务中实现先进的图像生成。
相关文章
|
4月前
|
存储 安全 前端开发
SpringBoot阿里云OSS文件上传实例
本文介绍了通过阿里云OSS实现用户头像上传的功能。首先概述了OSS的特点,如高持久性、RESTful API支持及多种存储类型。接着详细描述了前期准备步骤,包括注册阿里云账号、实名认证、开通OSS服务、创建Bucket及AccessKey。注意事项中提到需调整Bucket的访问权限以支持前端访问。最后提供了基于SpringBoot的代码实现,包含配置文件与上传工具类的编写,帮助开发者快速集成OSS文件上传功能。
956 1
SpringBoot阿里云OSS文件上传实例
|
12月前
|
存储 人工智能 开发工具
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
只需要通过向AI助理提问的方式输入您的需求,即可瞬间获得核心流程代码及参数,缩短学习路径、提升开发效率。
1676 5
AI助理化繁为简,速取代码参数——使用python SDK 处理OSS存储的图片
|
6月前
|
Java 开发工具 Spring
【Azure Application Insights】为Spring Boot应用集成Application Insight SDK
本文以Java Spring Boot项目为例,详细说明如何集成Azure Application Insights SDK以收集和展示日志。内容包括三步配置:1) 在`pom.xml`中添加依赖项`applicationinsights-runtime-attach`和`applicationinsights-core`;2) 在main函数中调用`ApplicationInsights.attach()`;3) 配置`applicationinsights.json`文件。同时提供问题排查建议及自定义日志方法示例,帮助用户顺利集成并使用Application Insights服务。
136 8
|
10月前
|
弹性计算 安全 开发工具
灵码评测-阿里云提供的ECS python3 sdk做安全组管理
批量变更阿里云ECS安全组策略(批量变更)
|
12月前
|
程序员 开发工具 Android开发
Android|使用阿里云推流 SDK 实现双路推流不同画面
本文记录了一种使用没有原生支持多路推流的阿里云推流 Android SDK,实现同时推送两路不同画面的流的方法。
210 7
|
Java 开发工具
通过Java SDK调用阿里云模型服务
在阿里云平台上,可以通过创建应用并使用模型服务完成特定任务,如生成文章内容。本示例展示了一段简化的Java代码,演示了如何调用阿里云模型服务生成关于“春秋战国经济与文化”的简短文章。示例代码通过设置系统角色为历史学家,并提出文章生成需求,最终处理并输出生成的文章内容。在实际部署前,请确保正确配置环境变量中的密钥和ID,并根据需要调整SDK导入语句及类名。更多详情和示例,请参考相关链接。
|
消息中间件 分布式计算 DataWorks
DataWorks产品使用合集之如何使用Python和阿里云SDK读取OSS中的文件
DataWorks作为一站式的数据开发与治理平台,提供了从数据采集、清洗、开发、调度、服务化、质量监控到安全管理的全套解决方案,帮助企业构建高效、规范、安全的大数据处理体系。以下是对DataWorks产品使用合集的概述,涵盖数据处理的各个环节。
|
网络协议 安全 调度
阿里云公共DNS发布支持鸿蒙系统版的SDK
阿里云公共DNS发布支持鸿蒙系统版SDK,赋能鸿蒙开发者快速接入阿里云公共DNS服务。公共DNS将帮助接入的鸿蒙应用免除LocalDNS劫持困扰、解析加速、精准调度。
|
算法 小程序 开发工具
视觉智能开放平台操作报错合集之同样的图片路径(上海阿里云),sdk报错code.400,是什么原因
在使用视觉智能开放平台时,可能会遇到各种错误和问题。虽然具体的错误代码和消息会因平台而异,但以下是一些常见错误类型及其可能的原因和解决策略的概述,包括但不限于:1. 认证错误、2. 请求参数错误、3. 资源超限、4. 图像质量问题、5. 服务不可用、6. 模型不支持的场景、7. 网络连接问题,这有助于快速定位和解决问题。
192 0
|
13天前
|
开发工具 Android开发
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡
177 11
X Android SDK file not found: adb.安卓开发常见问题-Android SDK 缺少 `adb`(Android Debug Bridge)-优雅草卓伊凡