Open rainit2006 opened 6 years ago
AmazonS3Client
■引用库
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.AccessControlList;
import com.amazonaws.services.s3.model.GroupGrantee;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.Permission;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectSummary;
■AmazonS3Clientを利用するコード例
import com.amazonaws.AmazonClientException;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3Object;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PreDestroy;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
@Component
public class SchemaEnumerator {
private static final Logger LOG = LoggerFactory.getLogger(SchemaEnumerator.class);
private final AmazonS3Client s3 = new AmazonS3Client();
@Value("${hive.schema.bucket}")
private String schemaDirectory;
@Value("${aws.active.schema.bucket}")
protected String activeSchemaBucket;
@Value("${aws.active.schema.key}")
protected String activeSchemaKey;
public SchemaEnumerator() {
s3.setRegion(Region.getRegion(Regions.EU_WEST_1));
}
@PreDestroy
private void shutdown() {
s3.shutdown();
}
/**
* Returns the schema that is considered as the preferred schema to run queries on, usually this is the latest one.
* It is configured externally so it can be easily changed by portal administrators.
*
* @return preferred schema
*/
public String getPreferredQueryBuilderSchema() {
String queryBuilderSchema;
try {
AmazonS3Client s3 = new AmazonS3Client();
S3Object activeSchema = s3.getObject(activeSchemaBucket, activeSchemaKey);
try (final InputStream is = activeSchema.getObjectContent();
final InputStreamReader reader = new InputStreamReader(is, "utf-8");
final BufferedReader br = new BufferedReader(reader)) {
queryBuilderSchema = br.readLine();
} catch (IOException e) {
LOG.error("Could not read from S3", e);
queryBuilderSchema = "";
}
} catch (AmazonClientException e) {
LOG.error("AmazonClientException when reading active-schema.txt", e);
queryBuilderSchema = "";
}
LOG.info("preferredQueryBuilderSchema='" + queryBuilderSchema + "'");
return queryBuilderSchema;
}
public List<String> getSchemas() {
boolean more;
List<String> schemas = new ArrayList<>();
ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName(schemaDirectory).withPrefix("schema-").withDelimiter("/"));
do {
for (String prefix : listing.getCommonPrefixes()) {
String schemaName = prefix.substring(0, prefix.length() - 1); // remove trailing /
schemas.add(schemaName);
}
more = listing.isTruncated();
if (more) {
listing = s3.listNextBatchOfObjects(listing);
}
} while (more);
return schemas;
}
}