如果我有两个aws服务帐户,每个帐户都可以访问s3存储桶(两个都不能访问)。有没有一种有效的方式在每个存储桶之间发送文件?
这是我到目前为止所拥有的:
System.setProperty(SDKGlobalConfiguration.DISABLE_CERT_CHECKING_SYSTEM_PROPERTY, "true");
//first account client
AWSCredentials credentialsFrom = new BasicAWSCredentials(fromAccessKey, fromSecretKey);
AmazonS3 s3ClientFrom = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentialsFrom)).withRegion(Regions.US_EAST_1).build();
//second account client
AWSCredentials credentialsTo = new BasicAWSCredentials(toAccessKey, toSecretKey);
AmazonS3 s3ClientTo = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentialsTo)).withRegion(Regions.US_EAST_1).build();
//get contents from first s3 client
ListObjectsRequest listObjectsRequestFrom = new ListObjectsRequest().withBucketName(fromBucket).withPrefix(fromPrefix).withDelimiter("/");
ObjectListing listingFrom = s3ClientFrom.listObjects(listObjectsRequestFrom);
//get each file from first s3 client
for (S3ObjectSummary objectSummary: listingFrom.getObjectSummaries()) {
S3Object s3Object = s3ClientFrom.getObject(new GetObjectRequest(fromBucket, objectSummary.getKey()));
//if current key is not a directory
if(!objectSummary.getKey().substring(objectSummary.getKey().length() - 1).equalsIgnoreCase("/")) {
//get name of the file by splitting the key/path by "/"
String[] fileName = objectSummary.getKey().split("[/]");
//put current file contents to new bucket
s3ClientTo.putObject(toBucket, toPrefix + fileName[fileName.length - 1], s3Object.getObjectContent(), s3Object.getObjectMetadata());
}
}
似乎问题中的代码片段是正确的方法。如果其他人有更好/更干净的解决方案,我将更新答案。谢谢xerx593的帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句