如果其他人想要的话,这个问题的答案对于一个教程来说已经足够密集了。现在,我将把它复制并粘贴到正确的论坛和帖子中以宣传:
没有太多的抱怨,我只想说,Camel 的强大,它的文档和示例库对于 AWS 世界的生产场景来说真的很缺乏...... 叹息...... 对于任何开源库来说,这都是一个拗口,而且可能是一个延伸。
我通过参考官方camel-s3 documentation 了解了如何解决凭证问题,首先了解如何创建高级 S3 配置(依赖于 aws sdk 本身——你可以在那里看到一个简单的例子——它使s3 客户端手动)。
在我弄清楚这一点后,我去了aws sdk documentation on IAM credentials 想知道这如何在 EC2 实例上工作,因为我能够自己构建客户端。在上述文档中,也有一些简单的示例。在使用列出的示例进行测试后,我发现凭证刷新(此问题的唯一目的)不起作用。一开始它可以获得凭据,但在我的测试期间手动过期后它没有刷新它们。
最后,我发现您可以指定一个提供程序链来自行处理凭据的刷新。解释这一点的 aws 文档是 here。
最后,我仍然需要为轮询 aws s3 存储桶的本地骆驼设置提供静态凭据,但是,我在 ec2s 上的远程环境可以使用可以完美刷新自身的临时凭据访问它们。哇! :)
为此,我简单地创建了一个工厂,该工厂使用本地骆驼设置进行本地开发和依赖临时 IAM 凭证的远程骆驼设置。这为我节省了安全问题和需要为所有远程环境手动刷新凭据的工作!
我不会解释如何创建工厂或如何完全设置我的本地和远程配置,但我将包含我的 AmazonS3ClientBuilder 代码示例,它为远程设置创建 S3 客户端。
AmazonS3ClientBuilder.standard()
.withCredentials(new InstanceProfileCredentialsProvider(false))
.withRegion(Regions.US_WEST_2)
.build();
如果我想知道我是如何做到这一点的,我可以提供一个示例项目来展示整个过程。
根据要求,这是我的 s3 客户端的本地和远程实现:
本地:
public class LocalAWSS3ClientManagerImpl implements AWSS3ClientManager {
private static Logger logger = LoggerFactory.getLogger(LocalAWSS3ClientManagerImpl.class);
private PriorityCodeSourcesRoutesProperties priorityCodeSourcesRoutesProperties;
private SimpleRegistry registry = new SimpleRegistry();
private CamelContext camelContext;
public LocalAWSS3ClientManagerImpl(PriorityCodeSourcesRoutesProperties priorityCodeSourcesRoutesProperties) {
this.priorityCodeSourcesRoutesProperties = priorityCodeSourcesRoutesProperties;
registry.put("amazonS3Client", getS3Client());
camelContext = new DefaultCamelContext(registry);
logger.info("Creating an AWS S3 manager for a local instance (you should not see this on AWS EC2s).");
}
private AmazonS3 getS3Client() {
try {
String awsBucketAccessKey = priorityCodeSourcesRoutesProperties.getAwsBucketAccessKey();
String awsBucketSecretKey = priorityCodeSourcesRoutesProperties.getAwsBucketSecretKey();
AWSCredentials awsCredentials = new BasicAWSCredentials(awsBucketAccessKey, awsBucketSecretKey);
return AmazonS3ClientBuilder.standard().withCredentials(
new AWSStaticCredentialsProvider(awsCredentials)).build();
} catch (RuntimeException ex) {
logger.error("Could not create AWS S3 client with the given credentials from the local config.");
}
return null;
}
public Endpoint getIncomingAWSEndpoint(final String incomingAWSBucket, final String region,
final String fileNameToSaveAndDownload) {
return camelContext.getEndpoint(
"aws-s3://" + incomingAWSBucket + "?" + "amazonS3Client=#amazonS3Client"
+ "®ion=" + region + "&deleteAfterRead=false" + "&prefix=" + fileNameToSaveAndDownload);
}
public Endpoint getOutgoingLocalEndpoint(final String outgoingEndpointDirectory,
final String fileNameToSaveAndDownload) {
return camelContext.getEndpoint(
"file://" + outgoingEndpointDirectory + "?" + "fileName="
+ fileNameToSaveAndDownload + "&readLock=markerFile");
}
}
远程:
public class RemoteAWSS3ClientManagerImpl implements AWSS3ClientManager {
private static Logger logger = LoggerFactory.getLogger(RemoteAWSS3ClientManagerImpl.class);
private PriorityCodeSourcesRoutesProperties priorityCodeSourcesRoutesProperties;
private SimpleRegistry registry = new SimpleRegistry();
private CamelContext camelContext;
public RemoteAWSS3ClientManagerImpl(PriorityCodeSourcesRoutesProperties priorityCodeSourcesRoutesProperties) {
this.priorityCodeSourcesRoutesProperties = priorityCodeSourcesRoutesProperties;
registry.put("amazonS3Client", getS3Client());
camelContext = new DefaultCamelContext(registry);
logger.info("Creating an AWS S3 client for a remote instance (normal for ec2s).");
}
private AmazonS3 getS3Client() {
try {
logger.info("Attempting to create an AWS S3 client with IAM role's temporary credentials.");
return AmazonS3ClientBuilder.standard()
.withCredentials(new InstanceProfileCredentialsProvider(false))
.withRegion(Regions.US_WEST_2)
.build();
} catch (RuntimeException ex) {
logger.error("Could not create AWS S3 client with the given credentials from the instance. "
+ "The default credential chain was used to create the AWS S3 client. "
+ ex.toString());
}
return null;
}
public Endpoint getIncomingAWSEndpoint(final String incomingAWSBucket, final String region,
final String fileNameToSaveAndDownload) {
return camelContext.getEndpoint(
"aws-s3://" + incomingAWSBucket + "?" + "amazonS3Client=#amazonS3Client"
+ "®ion=" + region + "&deleteAfterRead=false" + "&prefix=" + fileNameToSaveAndDownload);
}
public Endpoint getOutgoingLocalEndpoint(final String outgoingEndpointDirectory,
final String fileNameToSaveAndDownload) {
return camelContext.getEndpoint(
"file://" + outgoingEndpointDirectory + "?" + "fileName="
+ fileNameToSaveAndDownload + "&readLock=markerFile");
}
}