AWS sdk for s3 - S3 client

背景

在产品环境上通过 http 的方式访问 aws s3 是不安全的,需要使用aws sdk 提供的接口来访问

技术实现

项目中使用的是java

1. 在gradel 中引用对应的aws 包

implementation 'software.amazon.awssdk:s3:2.20.80' // aws sdk

implementation 'software.amazon.awssdk:sts:2.20.0'

2. 在gradel 中引用对应的aws 包

需要使用S3 Client 来向aws s3 发起请求,为了避免重复创建Client 冗余对象,创建工厂类来管理对应的 S3 Client 对象。扩展性也大幅提高。

此外,由于每一个Client 对象是一个单一的Region,需要不同的aws cluster_region 。

java 复制代码
public class S3ClientFactory {
    private static final Map<String, S3Client> clients = new ConcurrentHashMap<>();

    public static S3Client getClient(Region region) {
        return clients.computeIfAbsent(region.toString(), r -> S3Client.builder()
                .region(Region.of(r))
                .build());
    }

    public static String getCsvContent(String bucketName, String objectKey, String configKey) {
        // Create S3 Clients
        S3Client s3Stg = S3ClientFactory.getClient(Region.US_WEST_2);    // stage
        S3Client s3ProdNa = S3ClientFactory.getClient(Region.US_EAST_1); // prod NA

        // Select the appropriate S3 client based on configKey
        S3Client s3 = selectClient(configKey, s3Stg, s3ProdNa);

        // Prepare the GetObject request
        GetObjectRequest getObjectRequest = GetObjectRequest.builder()
                .bucket(bucketName)
                .key(objectKey)
                .build();

        // Fetch the object and read the content into a String
        try (ResponseInputStream<?> response = s3.getObject(getObjectRequest);
             BufferedReader reader = new BufferedReader(new InputStreamReader(response))) {

            // Collect all lines into a single string
            return reader.lines().collect(Collectors.joining("\n"));

        } catch (S3Exception e) {
            throw new CustomException("Failed to get AWS CSV by bucket and objectKey: " + e.awsErrorDetails().errorMessage(), e.getMessage());
        } catch (IOException e) {
            throw new CustomException("Error processing CSV content from AWS S3: " + e.getMessage(), e.getMessage());
        }
    }

    private static S3Client selectClient(String configKey, S3Client s3Stg, S3Client s3ProdNa) {
        if (configKey.contains("prod")) {
            return s3ProdNa; // Assuming s3ProdNa is used for NA and SA
        } else {
            return s3Stg; // Default to stage
        }
    }
}

展望

搞清楚 aws sdk 权限,access_api key,访问通信原理

相关推荐
运维行者_10 小时前
使用Applications Manager进行 Apache Solr 监控
运维·网络·数据库·网络安全·云计算·apache·solr
Britz_Kevin2 天前
从零开始的云计算生活——激流勇进,kubernetes模块之Pod资源对象
kubernetes·云计算·生活·#pod
阿湯哥2 天前
Cloud Computing(云计算)和Sky Computing(天空计算)
云计算
数据智能老司机2 天前
基于 Kubernetes 的平台工程——Kubernetes 上的平台化浪潮
kubernetes·云计算·devops
数据智能老司机3 天前
图算法趣味学——桥和割点
数据结构·算法·云计算
数据智能老司机3 天前
图算法趣味学——图遍历
数据结构·算法·云计算
苦逼IT运维3 天前
Jenkins + SonarQube 从原理到实战三:SonarQube 打通 Windows AD(LDAP)认证与踩坑记录
运维·服务器·windows·docker·云计算·jenkins·devops
在云上(oncloudai)3 天前
AWS KMS VS AWS Cloud HSM VS AWS Secret Manager?
云计算·aws
AKAMAI3 天前
通过Akamai分布式计算区域实现直播传输
人工智能·分布式·云计算
阿里云视频云3 天前
实战揭秘|魔搭社区 + 阿里云边缘云 ENS,快速部署大模型的落地实践
云计算·边缘计算·cdn