Java Code Examples for com.amazonaws.services.s3.model.S3Object#getKey()

The following examples show how to use com.amazonaws.services.s3.model.S3Object#getKey() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AWSSdkClient.java    From incubator-gobblin with Apache License 2.0 6 votes vote down vote up
/***
 * 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);
}
 
Example 2
Source File: ExtractionTools.java    From aws-codepipeline-plugin-for-jenkins with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 3
Source File: AmazonS3FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@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);
	}
}
 
Example 4
Source File: AmazonS3FileSystem.java    From iaf with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 5
Source File: Configuration.java    From bidder with Apache License 2.0 4 votes vote down vote up
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);
	}
 
Example 6
Source File: Configuration.java    From XRTB with Apache License 2.0 4 votes vote down vote up
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);
		}
	}
}
 
Example 7
Source File: AmazonS3FileSystem.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public String getName(S3Object f) {
	return f.getKey();
}
 
Example 8
Source File: AmazonS3FileSystem.java    From iaf with Apache License 2.0 4 votes vote down vote up
@Override
public String getCanonicalName(S3Object f) throws FileSystemException {
	return f.getBucketName() + f.getKey();
}