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

相关推荐
小白考证进阶中7 小时前
阿里云认证重要通知(Clouder+ACX职业岗位)
阿里云·云计算·阿里云acp报名·阿里云acp备考·阿里云aca证书·阿里云aca大模型·阿里云aca报名
spider_xcxc8 小时前
告别单点故障:阿里云CLB负载均衡从入门到实战
阿里云·云计算·负载均衡
hz5678912 小时前
电子远程评标系统哪家好?安全、合规、易用平台评测指南
安全·云计算·音视频·实时音视频·信息与通信
翼龙云_cloud14 小时前
腾讯云代理商: 腾讯云qGPU怎么破解算力浪费难题?
云计算·腾讯云·qgpu
阿乔外贸日记19 小时前
意大利进口主力产品及主要合作供应国
大数据·人工智能·物联网·搜索引擎·云计算
云服务器代理商19 小时前
AWS Bedrock 指南:模型接入、Knowledge Base RAG、Agents 与计费方式
云计算·aws·亚马逊云服务器·aws bedrock·aws代理·亚马逊云代理商
spider_xcxc19 小时前
阿里云NAS配置完全指南:从概念到实战
阿里云·云计算
我登哥MVP21 小时前
Hadoop成长史-从Nutch子项目到大数据生态王者
java·大数据·hadoop·分布式·云原生·云计算
我登哥MVP1 天前
HDFS硬核拆解-读写Pipeline与Java实战
java·hadoop·hdfs·云原生·云计算
Database_Cool_1 天前
HTAP数据库详解_阿里云PolarDB-X一体化行列存实现实时分析
分布式·阿里云·云计算