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

相关推荐
pp-周子晗(努力赶上课程进度版)1 小时前
Docker、Kubernetes与AWS中控机是什么?
docker·容器·kubernetes·aws
AWS官方合作商1 小时前
AWS云计算入门指南:从零到一,详解核心服务与免费套餐
云计算·aws
王道长服务器 | 亚马逊云1 小时前
AWS + 飞天CMS:高性能内容站的云端搭建方案
服务器·搜索引擎·aws
星释1 小时前
阿里云Windows数据2T以上磁盘扩容方案
阿里云·云计算
云资源服务商1 小时前
深度解析阿里云通用算力型U1与U2i实例性能差异:架构、算力、场景选型全对比
服务器·阿里云·云计算
大海绵啤酒肚11 小时前
OpenStack虚拟化平台之T版搭建部署
linux·运维·云计算·openstack
weixin_3077791311 小时前
基于AWS多区域部署的高可用性与灾难恢复架构设计
云计算·aws
我先去打把游戏先11 小时前
ESP32开发指南(基于IDF):连接AWS,乐鑫官方esp-aws-iot-master例程实验、跑通
开发语言·笔记·单片机·物联网·学习·云计算·aws
uuukashiro12 小时前
探索腾讯云对象存储产品:COS的卓越特性与功能
ai·云计算·腾讯云
算家计算15 小时前
5年后手机和APP将成历史?马斯克最新预言背后:端云协同与AI操作系统的未来架构
人工智能·云计算·资讯