Search in sources :

Example 1 with AmazonS3ClientBuilder.defaultClient

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));
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsRequest(com.amazonaws.services.s3.model.ListObjectsRequest) S3ObjectInfo(com.amazonaws.services.neptune.util.S3ObjectInfo) ObjectListing(com.amazonaws.services.s3.model.ObjectListing)

Example 2 with AmazonS3ClientBuilder.defaultClient

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);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) Matcher(java.util.regex.Matcher) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) ByteArrayInputStream(java.io.ByteArrayInputStream) S3EventNotificationRecord(com.amazonaws.services.s3.event.S3EventNotification.S3EventNotificationRecord) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 3 with AmazonS3ClientBuilder.defaultClient

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;
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ArrayList(java.util.ArrayList) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object)

Example 4 with AmazonS3ClientBuilder.defaultClient

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.");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 5 with AmazonS3ClientBuilder.defaultClient

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);
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonServiceException(com.amazonaws.AmazonServiceException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)14 AmazonServiceException (com.amazonaws.AmazonServiceException)7 S3Object (com.amazonaws.services.s3.model.S3Object)3 S3ObjectInfo (com.amazonaws.services.neptune.util.S3ObjectInfo)2 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)2 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)2 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 AWSCredentials (com.amazonaws.auth.AWSCredentials)1 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)1 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)1 NoSuchEntityException (com.amazonaws.services.identitymanagement.model.NoSuchEntityException)1 AmazonPinpoint (com.amazonaws.services.pinpoint.AmazonPinpoint)1 ExportJournalToS3Result (com.amazonaws.services.qldb.model.ExportJournalToS3Result)1 InvalidParameterException (com.amazonaws.services.qldb.model.InvalidParameterException)1 AmazonRekognition (com.amazonaws.services.rekognition.AmazonRekognition)1 BoundingBox (com.amazonaws.services.rekognition.model.BoundingBox)1