package org.itrunner.aws.s3;
import com.amazonaws.HttpMethod;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.*;
import java.io.File;
import java.net.URL;
import java.util.Date;
import java.util.List;
public class S3Util {
private static AmazonS3 s3;
static {
s3 = AmazonS3ClientBuilder.standard().withRegion(Regions.CN_NORTH_1).build();
}
private S3Util() {
}
/*
* Create a new S3 bucket - Amazon S3 bucket names are globally unique
*/
public static Bucket createBucket(String bucketName) {
return s3.createBucket(bucketName);
}
/*
* List the buckets in your account
*/
public static List listBuckets() {
return s3.listBuckets();
}
/*
* List objects in your bucket
*/
public static ObjectListing listObjects(String bucketName) {
return s3.listObjects(bucketName);
}
/*
* List objects in your bucket by prefix
*/
public static ObjectListing listObjects(String bucketName, String prefix) {
return s3.listObjects(bucketName, prefix);
}
/*
* Upload an object to your bucket
*/
public static PutObjectResult putObject(String bucketName, String key, File file) {
return s3.putObject(bucketName, key, file);
}
/*
* Download an object - When you download an object, you get all of the object's metadata and a stream from which to read the contents.
* It's important to read the contents of the stream as quickly as possibly since the data is streamed directly from Amazon S3 and your
* network connection will remain open until you read all the data or close the input stream.
*/
public static S3Object get(String bucketName, String key) {
return s3.getObject(bucketName, key);
}
/*
* Delete an object - Unless versioning has been turned on for your bucket, there is no way to undelete an object, so use caution when deleting objects.
*/
public static void deleteObject(String bucketName, String key) {
s3.deleteObject(bucketName, key);
}
/*
* Delete a bucket - A bucket must be completely empty before it can be deleted, so remember to delete any objects from your buckets before
* you try to delete them.
*/
public static void deleteBucket(String bucketName) {
s3.deleteBucket(bucketName);
}
} 生成预签名URL
默认,S3对象为私有,只有所有者具有访问权限。但是,对象所有者可以使用自己的安全凭证来创建预签名的URL,授予有限时间内的对象下载许可,从而与其他用户共享对象,收到预签名URL的任何人都可以访问对象。
当创建预签名URL时,必须提供安全凭证、存储桶名称和对象键、HTTP 方法 (指定为GET来下载对象) 和过期时间。
public String generatePresignedUrl(String bucketName, String key, int minutes) {
// Sets the expiration date
Date expiration = new Date();
long expTimeMillis = expiration.getTime();
expTimeMillis += 1000 * 60 * minutes;
expiration.setTime(expTimeMillis);
// Generate the presigned URL.
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key).withMethod(HttpMethod.GET).withExpiration(expiration);
URL url = s3.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
} 从对象中选择内容
利用Amazon S3 Select,可以使用SQL语句筛选 S3 对象的内容,检索所需的部分数据。Amazon S3 Select 适用于以CSV或JSON格式存储的对象,这些对象可以通过GZIP或BZIP2压缩和服务器端加密。
select s.email from S3Object s where s.username='Jason'
更多SQL信息请查看Amazon S3 Select 和 Amazon Glacier Select 的 SQL 参考。
查询CSV文件
以下示例将查询结果保存在outputPath文件中:
public static void selectCsvObjectContent(String bucketName, String csvObjectKey, String sql, String outputPath) throws Exception {
SelectObjectContentRequest request = generateBaseCSVRequest(bucketName, csvObjectKey, sql);
final AtomicBoolean isResultComplete = new AtomicBoolean(false);
try (OutputStream fileOutputStream = new FileOutputStream(new File(outputPath));
SelectObjectContentResult result = s3.selectObjectContent(request)) {
InputStream resultInputStream = result.getPayload().getRecordsInputStream(
new SelectObjectContentEventVisitor() {
/*
* An End Event informs that the request has finished successfully.
*/
@Override
public void visit(SelectObjectContentEvent.EndEvent event) {
isResultComplete.set(true);
}
}
);
copy(resultInputStream, fileOutputStream);
}
/*
* The End Event indicates all matching records have been transmitted. If the End Event is not received, the results may be incomplete.
*/
if (!isResultComplete.get()) {
throw new Exception("S3 Select request was incomplete as End Event was not received.");
}
}
private static SelectObjectContentRequest generateBaseCSVRequest(String bucket, String key, String query) {
SelectObjectContentRequest request = new SelectObjectContentRequest();
request.setBucketName(bucket);
request.setKey(key);
request.setExpression(query);
request.setExpressionType(ExpressionType.SQL);
InputSerialization inputSerialization = new InputSerialization();
CSVInput csvInput = new CSVInput();
csvInput.setFileHeaderInfo(FileHeaderInfo.USE);
inputSerialization.setCsv(csvInput);
inputSerialization.setCompressionType(CompressionType.NONE);
request.setInputSerialization(inputSerialization);
OutputSerialization outputSerialization = new OutputSerialization();
outputSerialization.setCsv(new CSVOutput());
request.setOutputSerialization(outputSerialization);
return request;
} 参考文档
Amazon Simple Storage Service Documentation
Working with Amazon S3 Objects
Using the SDK
Programming Examples
Generate a Pre-signed Object URL using AWS SDK for Java
AWS Java Sample