下面列出了com.amazonaws.services.s3.model.S3Object#getKey ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/***
* Download a S3 object to local directory
*
* @param s3ObjectSummary S3 object summary for the object to download
* @param targetDirectory Local target directory to download the object to
* @throws IOException If any errors were encountered in downloading the object
*/
public void downloadS3Object(S3ObjectSummary s3ObjectSummary,
String targetDirectory)
throws IOException {
final AmazonS3 amazonS3 = getS3Client();
final GetObjectRequest getObjectRequest = new GetObjectRequest(
s3ObjectSummary.getBucketName(),
s3ObjectSummary.getKey());
final S3Object s3Object = amazonS3.getObject(getObjectRequest);
final String targetFile = StringUtils.removeEnd(targetDirectory, File.separator) + File.separator + s3Object.getKey();
FileUtils.copyInputStreamToFile(s3Object.getObjectContent(), new File(targetFile));
LOGGER.info("S3 object downloaded to file: " + targetFile);
}
public static CompressionType getCompressionType(final S3Object sessionObject, final TaskListener l) {
final String key = sessionObject.getKey();
CompressionType compressionType = CompressionType.None;
if (endsWithLowerCase(key, ".zip")) {
compressionType = CompressionType.Zip;
} else if (endsWithLowerCase(key, ".tar.gz")) {
compressionType = CompressionType.TarGz;
} else if (endsWithLowerCase(key, ".tar")) {
compressionType = CompressionType.Tar;
}
if (compressionType == CompressionType.None) {
final String contentType = sessionObject.getObjectMetadata().getContentType();
if ("application/zip".equalsIgnoreCase(contentType)) {
compressionType = CompressionType.Zip;
} else if ("application/gzip".equalsIgnoreCase(contentType)
|| "application/x-gzip".equalsIgnoreCase(contentType)) {
compressionType = CompressionType.TarGz;
} else if ("application/tar".equalsIgnoreCase(contentType)
|| "application/x-tar".equalsIgnoreCase(contentType)) {
compressionType = CompressionType.Tar;
}
}
LoggingHelper.log(l, "Detected compression type: %s", compressionType.name());
return compressionType;
}
@Override
public void deleteFile(S3Object f) throws FileSystemException {
try {
DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, f.getKey());
s3Client.deleteObject(deleteObjectRequest);
} catch (AmazonServiceException e) {
throw new FileSystemException(e);
}
}
@Override
public S3Object copyFile(S3Object f, String destinationFolder, boolean createFolder) throws FileSystemException {
String destinationFile = destinationFolder+"/"+f.getKey();
if(s3Client.doesObjectExist(bucketName, destinationFile)) {
throw new FileSystemException("Cannot copy file. Destination file already exists.");
}
s3Client.copyObject(bucketName, f.getKey(), bucketName, destinationFile);
return toFile(destinationFile);
}
public void processDirectory(AmazonS3 s3, ObjectListing listing, String bucket) throws Exception {
double time = System.currentTimeMillis();
ExecutorService executor = Executors.newFixedThreadPool(16);
int count = 0;
for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
if ("STANDARD".equalsIgnoreCase(objectSummary.getStorageClass())) {
long size = objectSummary.getSize();
logger.debug("*** Processing S3 {}, size: {}", objectSummary.getKey(), size);
S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));
String bucketName = object.getBucketName();
String keyName = object.getKey();
GetObjectTaggingRequest request = new GetObjectTaggingRequest(bucketName, keyName);
GetObjectTaggingResult result = s3.getObjectTagging(request);
List<Tag> tags = result.getTagSet();
String type = null;
String name = null;
if (tags.isEmpty()) {
object.close();
logger.warn("Error, S3 object: {} has no tags", keyName);
} else {
for (Tag tag : tags) {
String key = tag.getKey();
String value = tag.getValue();
if (key.equals("type")) {
type = value;
}
if (key.equals("name")) {
name = value;
}
}
if (name == null) {
object.close();
throw new Exception("Error: " + keyName + " is missing a name tag");
}
if (name.contains(" ")) {
object.close();
throw new Exception("Error: " + keyName + " has a name attribute with a space in it");
}
if (type == null) {
object.close();
throw new Exception("Error: " + keyName + " has no type tag");
}
if (!name.startsWith("$"))
name = "$" + name;
// The runnable will call object.close();
Runnable w = new AwsWorker(type, name, object, size);
executor.execute(w);
count++;
}
}
}
executor.shutdown();
executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
time = System.currentTimeMillis() - time;
time = time / 60000;
logger.info("Initialized all {} S3 objects in {} minutes", count, time);
}
public void processDirectory(AmazonS3Client s3, ObjectListing listing, String bucket) throws Exception {
for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
long size = objectSummary.getSize();
logger.info("*** Processing S3 {}, size: {}",objectSummary.getKey(),size);
S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));
String bucketName = object.getBucketName();
String keyName = object.getKey();
GetObjectTaggingRequest request = new GetObjectTaggingRequest(bucketName, keyName);
GetObjectTaggingResult result = s3.getObjectTagging(request);
List<Tag> tags = result.getTagSet();
String type = null;
String name = null;
if (tags.isEmpty()) {
System.err.println("Error: " + keyName + " has no tags");
} else {
for (Tag tag : tags) {
String key = tag.getKey();
String value = tag.getValue();
if (key.equals("type")) {
type = value;
}
if (key.equals("name")) {
name = value;
}
}
if (name == null)
throw new Exception("Error: " + keyName + " is missing a name tag");
if (name.contains(" "))
throw new Exception("Error: " + keyName + " has a name attribute with a space in it");
if (type == null)
throw new Exception("Error: " + keyName + " has no type tag");
if (!name.startsWith("$"))
name = "$" + name;
readData(type, name, object, size);
}
}
}
@Override
public String getName(S3Object f) {
return f.getKey();
}
@Override
public String getCanonicalName(S3Object f) throws FileSystemException {
return f.getBucketName() + f.getKey();
}