use of com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient in project amazon-neptune-tools by awslabs.
the class NeptuneExportService method checkS3OutputIsEmpty.
private void checkS3OutputIsEmpty() {
AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
S3ObjectInfo s3ObjectInfo = new S3ObjectInfo(outputS3Path);
ObjectListing listing = s3.listObjects(new ListObjectsRequest(s3ObjectInfo.bucket(), s3ObjectInfo.key(), null, null, 1));
if (!listing.getObjectSummaries().isEmpty()) {
throw new IllegalStateException(String.format("S3 destination contains existing objects: %s. Set 'overwriteExisting' parameter to 'true' to allow overwriting existing objects.", outputS3Path));
}
}
use of com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient in project aws-lambda-developer-guide by awsdocs.
the class Handler method handleRequest.
@Override
public String handleRequest(S3Event s3event, Context context) {
try {
logger.info("EVENT: " + gson.toJson(s3event));
S3EventNotificationRecord record = s3event.getRecords().get(0);
String srcBucket = record.getS3().getBucket().getName();
// Object key may have spaces or unicode non-ASCII characters.
String srcKey = record.getS3().getObject().getUrlDecodedKey();
String dstBucket = srcBucket;
String dstKey = "resized-" + srcKey;
// Infer the image type.
Matcher matcher = Pattern.compile(".*\\.([^\\.]*)").matcher(srcKey);
if (!matcher.matches()) {
logger.info("Unable to infer image type for key " + srcKey);
return "";
}
String imageType = matcher.group(1);
if (!(JPG_TYPE.equals(imageType)) && !(PNG_TYPE.equals(imageType))) {
logger.info("Skipping non-image " + srcKey);
return "";
}
// Download the image from S3 into a stream
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
S3Object s3Object = s3Client.getObject(new GetObjectRequest(srcBucket, srcKey));
InputStream objectData = s3Object.getObjectContent();
// Read the source image
BufferedImage srcImage = ImageIO.read(objectData);
int srcHeight = srcImage.getHeight();
int srcWidth = srcImage.getWidth();
// Infer the scaling factor to avoid stretching the image
// unnaturally
float scalingFactor = Math.min(MAX_WIDTH / srcWidth, MAX_HEIGHT / srcHeight);
int width = (int) (scalingFactor * srcWidth);
int height = (int) (scalingFactor * srcHeight);
BufferedImage resizedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = resizedImage.createGraphics();
// Fill with white before applying semi-transparent (alpha) images
g.setPaint(Color.white);
g.fillRect(0, 0, width, height);
// Simple bilinear resize
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.drawImage(srcImage, 0, 0, width, height, null);
g.dispose();
// Re-encode image to target format
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(resizedImage, imageType, os);
InputStream is = new ByteArrayInputStream(os.toByteArray());
// Set Content-Length and Content-Type
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(os.size());
if (JPG_TYPE.equals(imageType)) {
meta.setContentType(JPG_MIME);
}
if (PNG_TYPE.equals(imageType)) {
meta.setContentType(PNG_MIME);
}
// Uploading to S3 destination bucket
logger.info("Writing to: " + dstBucket + "/" + dstKey);
try {
s3Client.putObject(dstBucket, dstKey, is, meta);
} catch (AmazonServiceException e) {
logger.error(e.getErrorMessage());
System.exit(1);
}
logger.info("Successfully resized " + srcBucket + "/" + srcKey + " and uploaded to " + dstBucket + "/" + dstKey);
return "Ok";
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient in project aws-doc-sdk-examples by awsdocs.
the class ListEndpointIds method getEndpointIds.
private static List<String> getEndpointIds(String s3bucketName, List<String> endpointFileKeys) {
List<String> endpointIds = new ArrayList<>();
// Initializes the Amazon S3 client.
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
// Gets the endpoint IDs from the exported endpoints files.
try {
for (String key : endpointFileKeys) {
S3Object endpointFile = s3Client.getObject(s3bucketName, key);
endpointIds.addAll(getEndpointIdsFromFile(endpointFile));
}
} catch (AmazonServiceException e) {
System.err.println(e.getMessage());
System.exit(1);
}
return endpointIds;
}
use of com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient in project aws-doc-sdk-examples by awsdocs.
the class ListEndpointIds method deleteS3Objects.
private static void deleteS3Objects(String s3BucketName, List<String> keys) {
AmazonS3 s3Client = AmazonS3ClientBuilder.defaultClient();
String[] keysArray = keys.toArray(new String[keys.size()]);
DeleteObjectsRequest request = new DeleteObjectsRequest(s3BucketName).withKeys(keysArray);
System.out.println("Deleting the following Amazon S3 objects created by Amazon Pinpoint:");
for (String key : keys) {
System.out.println("\t- " + key);
}
try {
s3Client.deleteObjects(request);
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
System.out.println("Finished deleting objects.");
}
use of com.amazonaws.services.s3.AmazonS3ClientBuilder.defaultClient in project aws-doc-sdk-examples by awsdocs.
the class ImportEndpoints method uploadToS3.
private static void uploadToS3(File endpointsFile, String s3BucketName) {
// Initializes Amazon S3 client.
final AmazonS3 s3 = AmazonS3ClientBuilder.defaultClient();
// Checks whether the specified bucket exists. If not, attempts to create one.
if (!s3.doesBucketExistV2(s3BucketName)) {
try {
s3.createBucket(s3BucketName);
System.out.format("Created S3 bucket %s.\n", s3BucketName);
} catch (AmazonS3Exception e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
// Uploads the endpoints file to the bucket.
String endpointsFileName = endpointsFile.getName();
System.out.format("Uploading %s to S3 bucket %s . . .\n", endpointsFileName, s3BucketName);
try {
s3.putObject(s3BucketName, "imports/" + endpointsFileName, endpointsFile);
System.out.println("Finished uploading to S3.");
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
}
}
Aggregations