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,访问通信原理

相关推荐
翼龙云_cloud9 小时前
亚马逊云服务器服务代理商:Bedrock 实践经验 Serverless 架构下的 AI 自动化落地
服务器·架构·serverless·云计算·aws
GPU实战笔记11 小时前
实例节点选择:创建入口与运行时稳定性的边界
云计算·a100·gpu 实例·节点选择·工作流验证
mengge.cloud16 小时前
1019Namespace ,Contexts ,Kubernetes Pod
运维·服务器·网络·云原生·容器·kubernetes·云计算
云老大-阿里云国际站代理商21 小时前
网站遇到恶意爬虫和CC攻击,阿里云国际站WAF与Bot管理怎么搭配
爬虫·阿里云·云计算
着迷不白1 天前
生产环境标准的Linux系统安装到服务器
linux·云计算
Geek-Chow2 天前
01 · Why AWS Lambda: Idle Cost, Capacity Planning and Risky In-Place Deploys
云计算
workflower2 天前
超算中心聚焦高性能计算场景(HPC)
大数据·网络·人工智能·机器人·云计算·汽车
workflower2 天前
Agentic AI 时代的企业ICT 基础设施运维
运维·人工智能·机器学习·重构·云计算·汽车
Geek-Chow3 天前
00 · AWS Lambda 心智模型学习指南:总览、思维导图与阅读路线(专栏目录)
云计算
Geek-Chow3 天前
07 · AWS Lambda 自测题与动手实验:用 AWS CLI 验证版本、别名、预置并发与冷启动
云计算