[ server ] [ oss ] - OSS配置 动态获取
This commit is contained in:
@@ -2,7 +2,7 @@ package io.wdd.func.controller;
|
||||
|
||||
import io.swagger.annotations.Api;
|
||||
import io.swagger.annotations.ApiOperation;
|
||||
import io.wdd.func.oss.oracle.service.OracleOSSCoreService;
|
||||
import io.wdd.func.oss.service.OSSCoreService;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
@@ -15,13 +15,13 @@ import javax.annotation.Resource;
|
||||
public class OSSController {
|
||||
|
||||
@Resource
|
||||
OracleOSSCoreService oracleOSSCoreService;
|
||||
OSSCoreService OSSCoreService;
|
||||
|
||||
@GetMapping("/bucket/list/all")
|
||||
@ApiOperation("列出所有的桶")
|
||||
public void bucketListAll() {
|
||||
|
||||
oracleOSSCoreService.listBucketList();
|
||||
OSSCoreService.listBucketList();
|
||||
System.out.println(" = ");
|
||||
}
|
||||
|
||||
|
||||
@@ -1,27 +1,42 @@
|
||||
package io.wdd.func.oss.oracle.config;
|
||||
package io.wdd.func.oss.config;
|
||||
|
||||
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
|
||||
import com.amazonaws.auth.AWSCredentialsProvider;
|
||||
import com.amazonaws.auth.AWSStaticCredentialsProvider;
|
||||
import com.amazonaws.auth.BasicAWSCredentials;
|
||||
import com.amazonaws.client.builder.AwsClientBuilder;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.AmazonS3Client;
|
||||
import kotlin.PublishedApi;
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 从Nacos中获取配置,获取所有S3的配置,并且自动更新
|
||||
* <p>
|
||||
* https://developer.aliyun.com/article/1111261
|
||||
* https://www.cnblogs.com/cndarren/p/16833526.html
|
||||
*/
|
||||
@Configuration
|
||||
public class OracleOSSConfiguration {
|
||||
@ConfigurationProperties(prefix = "oss")
|
||||
@Data
|
||||
@RefreshScope
|
||||
public class OSSConfiguration {
|
||||
|
||||
/**
|
||||
* 缓存所有的S3客户端
|
||||
*/
|
||||
public List<AmazonS3> ALL_S3_CLIENT = new ArrayList<>();
|
||||
|
||||
|
||||
/**
|
||||
* 从Nacos中获取到所有的对象存储对象
|
||||
*/
|
||||
@@ -33,10 +48,14 @@ public class OracleOSSConfiguration {
|
||||
String Seoul2Key;
|
||||
@Value("${oss.oracle.seoul2.secret}")
|
||||
String Seoul2Secret;
|
||||
private LinkedHashMap<String, LinkedHashMap<String, String>> oracle = new LinkedHashMap<>();
|
||||
|
||||
|
||||
@PostConstruct
|
||||
public void buildAllS3Client() {
|
||||
|
||||
System.out.println("oracle = " + oracle);
|
||||
|
||||
AWSCredentialsProvider credentials = new AWSStaticCredentialsProvider(new BasicAWSCredentials(
|
||||
Seoul2Key,
|
||||
Seoul2Secret
|
||||
@@ -1,6 +0,0 @@
|
||||
package io.wdd.func.oss.oracle.service;
|
||||
|
||||
public interface OracleOSSCoreService {
|
||||
|
||||
void listBucketList();
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
package io.wdd.func.oss.oracle.service;
|
||||
|
||||
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import io.wdd.func.oss.oracle.config.OracleOSSConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* https://docs.oracle.com/en-us/iaas/Content/Object/Tasks/s3compatibleapi.htm#usingAPI
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OracleOSSCoreServiceImpl implements OracleOSSCoreService {
|
||||
|
||||
|
||||
@Resource
|
||||
OracleOSSConfiguration ossConfiguration;
|
||||
|
||||
|
||||
@Override
|
||||
public void listBucketList() {
|
||||
|
||||
AmazonS3 amazonS3 = ossConfiguration.ALL_S3_CLIENT.get(0);
|
||||
|
||||
List<Bucket> buckets = amazonS3.listBuckets();
|
||||
|
||||
|
||||
System.out.println("buckets = " + buckets);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package io.wdd.func.oss.service;
|
||||
|
||||
public interface OSSCoreService {
|
||||
|
||||
/**
|
||||
* 对桶的操作
|
||||
*/
|
||||
|
||||
/**
|
||||
* 列出所有的桶
|
||||
* */
|
||||
void listBucketList();
|
||||
|
||||
|
||||
void deleteBucket();
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package io.wdd.func.oss.service;
|
||||
|
||||
|
||||
import com.alibaba.nacos.api.config.annotation.NacosConfigListener;
|
||||
import com.amazonaws.services.s3.AmazonS3;
|
||||
import com.amazonaws.services.s3.model.Bucket;
|
||||
import io.wdd.func.oss.config.OSSConfiguration;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.yaml.snakeyaml.Yaml;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* https://docs.oracle.com/en-us/iaas/Content/Object/Tasks/s3compatibleapi.htm#usingAPI
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class OSSCoreServiceImpl implements OSSCoreService {
|
||||
|
||||
|
||||
@Resource
|
||||
OSSConfiguration ossConfiguration;
|
||||
|
||||
|
||||
/**
|
||||
* 使用Linstener 更新OSS的配置信息
|
||||
* @param content 获取到的更新的Nacos
|
||||
*/
|
||||
@NacosConfigListener(dataId = "octopus-server-k3s.yaml", groupId = "k3s")
|
||||
public void listener(String content){
|
||||
|
||||
System.out.println("content = " + content);
|
||||
|
||||
Yaml yaml = new Yaml();
|
||||
|
||||
Map<String, Object> contextMap = yaml.load(content);
|
||||
|
||||
Map<String, Object> ossObject = (Map<String, Object>) contextMap.get("oss");
|
||||
LinkedHashMap<String, LinkedHashMap<String, String>> map = (LinkedHashMap<String, LinkedHashMap<String, String>>) ossObject.get("oralce");
|
||||
|
||||
// 需要同步这些
|
||||
// synchronized (oracle) {
|
||||
//
|
||||
// oracle.clear();
|
||||
// oracle.putAll(map);
|
||||
//
|
||||
// }
|
||||
|
||||
System.out.println("map = " + map);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void listBucketList() {
|
||||
|
||||
AmazonS3 amazonS3 = ossConfiguration.ALL_S3_CLIENT.get(0);
|
||||
|
||||
List<Bucket> buckets = amazonS3.listBuckets();
|
||||
|
||||
|
||||
System.out.println("buckets = " + buckets);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteBucket() {
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user