阿里云RAM凭据插件应用纪实
官方传送
-
官方文档传送门
-
官方源码传送门
记录日期
2023-06-13
背景简介
项目中主要使用了OSS,本文记录在OSS SDK中的使用方法
引入依赖
<dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-coreartifactId><version>4.5.17version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>[2.1.0,3.10.2]version>
<exclusions><exclusion><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-kmsartifactId>exclusion>
exclusions>
dependency>
<dependency><groupId>com.aliyungroupId><artifactId>aliyun-sdk-oss-managed-credentials-providerartifactId><version>1.2.3version>
dependency>
实际在我的项目中引入aliyun-java-sdk-core出现了一些冲突,需要在依赖中排除如下,仅供参考:
<dependency><groupId>com.aliyungroupId><artifactId>aliyun-java-sdk-coreartifactId><version>4.5.17version><exclusions><exclusion><groupId>org.ini4jgroupId><artifactId>ini4jartifactId>exclusion><exclusion><groupId>io.opentracinggroupId><artifactId>opentracing-utilartifactId>exclusion>exclusions>dependency>
配置文件
通过配置文件managed_credentials_providers.properties或者环境变量获取client key凭证口令,此处使用配置文件:
cache_client_dkms_config_info=[{"regionId":"<your kms region>","endpoint":"<your kms endpoint>","passwordFromFilePath":"<your client key password file absolute path>","clientKeyFile":"<your client key file absolute path>","ignoreSslCerts":false,"caFilePath":"<your DKMS's CA certificate file absolute path>"}]
其中client key文件、凭证口令和CA证书提前上传服务器,配置文件中可以使用绝对路径。
需要注意配置文件读取的位置是和Jar包同级的目录下:
- /home/service/kms-test
- kms-test-0.0.1-SNAPSHOT.jar
- managed_credentials_providers.properties
代码调用
原先通过AK/SK获取OSS客户端的代码为:
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, secret);
切换为RAM凭据后获取OSS客户端代码为:
OSS ossClient = new ProxyOSSClientBuilder().build(endpoint, secretName);
其中secretName为RAM凭据名称(阿里云控制台 - 密钥管理服务 - 凭据管理 - RAM凭据)。
其他代码均无需改变。
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
