Search in sources :

Example 6 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project camel by apache.

the class AmazonS3ClientMock method putObject.

@SuppressWarnings("resource")
@Override
public PutObjectResult putObject(PutObjectRequest putObjectRequest) throws AmazonClientException, AmazonServiceException {
    putObjectRequests.add(putObjectRequest);
    S3Object s3Object = new S3Object();
    s3Object.setBucketName(putObjectRequest.getBucketName());
    s3Object.setKey(putObjectRequest.getKey());
    if (putObjectRequest.getFile() != null) {
        try {
            s3Object.setObjectContent(new FileInputStream(putObjectRequest.getFile()));
        } catch (FileNotFoundException e) {
            throw new AmazonServiceException("Cannot store the file object.", e);
        }
    } else {
        s3Object.setObjectContent(putObjectRequest.getInputStream());
    }
    objects.add(s3Object);
    PutObjectResult putObjectResult = new PutObjectResult();
    putObjectResult.setETag("3a5c8b1ad448bca04584ecb55b836264");
    return putObjectResult;
}
Also used : PutObjectResult(com.amazonaws.services.s3.model.PutObjectResult) FileNotFoundException(java.io.FileNotFoundException) AmazonServiceException(com.amazonaws.AmazonServiceException) S3Object(com.amazonaws.services.s3.model.S3Object) FileInputStream(java.io.FileInputStream)

Example 7 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project camel by apache.

the class S3BatchConsumerMaxMessagesPerPollTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    AmazonS3ClientMock clientMock = new AmazonS3ClientMock();
    // add 20 messages
    for (int counter = 0; counter < 20; counter++) {
        S3Object s3Object = new S3Object();
        s3Object.setBucketName("mycamelbucket");
        s3Object.setKey("counter-" + counter);
        clientMock.objects.add(s3Object);
    }
    registry.bind("amazonS3Client", clientMock);
    return registry;
}
Also used : JndiRegistry(org.apache.camel.impl.JndiRegistry) S3Object(com.amazonaws.services.s3.model.S3Object) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 8 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project gradle-s3-build-cache by myniva.

the class AwsS3BuildCacheService method load.

@Override
public boolean load(BuildCacheKey key, BuildCacheEntryReader reader) {
    final String bucketPath = getBucketPath(key);
    if (s3.doesObjectExist(bucketName, bucketPath)) {
        logger.info("Found cache item '{}' in S3 bucket", bucketPath);
        S3Object object = s3.getObject(bucketName, bucketPath);
        try (InputStream is = object.getObjectContent()) {
            reader.readFrom(is);
            return true;
        } catch (IOException e) {
            throw new BuildCacheException("Error while reading cache object from S3 bucket", e);
        }
    } else {
        logger.info("Did not find cache item '{}' in S3 bucket", bucketPath);
        return false;
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BuildCacheException(org.gradle.caching.BuildCacheException) S3Object(com.amazonaws.services.s3.model.S3Object) IOException(java.io.IOException)

Example 9 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project nifi by apache.

the class FetchS3Object method onTrigger.

@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) {
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }
    final long startNanos = System.nanoTime();
    final String bucket = context.getProperty(BUCKET).evaluateAttributeExpressions(flowFile).getValue();
    final String key = context.getProperty(KEY).evaluateAttributeExpressions(flowFile).getValue();
    final String versionId = context.getProperty(VERSION_ID).evaluateAttributeExpressions(flowFile).getValue();
    final AmazonS3 client = getClient();
    final GetObjectRequest request;
    if (versionId == null) {
        request = new GetObjectRequest(bucket, key);
    } else {
        request = new GetObjectRequest(bucket, key, versionId);
    }
    final Map<String, String> attributes = new HashMap<>();
    try (final S3Object s3Object = client.getObject(request)) {
        flowFile = session.importFrom(s3Object.getObjectContent(), flowFile);
        attributes.put("s3.bucket", s3Object.getBucketName());
        final ObjectMetadata metadata = s3Object.getObjectMetadata();
        if (metadata.getContentDisposition() != null) {
            final String fullyQualified = metadata.getContentDisposition();
            final int lastSlash = fullyQualified.lastIndexOf("/");
            if (lastSlash > -1 && lastSlash < fullyQualified.length() - 1) {
                attributes.put(CoreAttributes.PATH.key(), fullyQualified.substring(0, lastSlash));
                attributes.put(CoreAttributes.ABSOLUTE_PATH.key(), fullyQualified);
                attributes.put(CoreAttributes.FILENAME.key(), fullyQualified.substring(lastSlash + 1));
            } else {
                attributes.put(CoreAttributes.FILENAME.key(), metadata.getContentDisposition());
            }
        }
        if (metadata.getContentMD5() != null) {
            attributes.put("hash.value", metadata.getContentMD5());
            attributes.put("hash.algorithm", "MD5");
        }
        if (metadata.getContentType() != null) {
            attributes.put(CoreAttributes.MIME_TYPE.key(), metadata.getContentType());
        }
        if (metadata.getETag() != null) {
            attributes.put("s3.etag", metadata.getETag());
        }
        if (metadata.getExpirationTime() != null) {
            attributes.put("s3.expirationTime", String.valueOf(metadata.getExpirationTime().getTime()));
        }
        if (metadata.getExpirationTimeRuleId() != null) {
            attributes.put("s3.expirationTimeRuleId", metadata.getExpirationTimeRuleId());
        }
        if (metadata.getUserMetadata() != null) {
            attributes.putAll(metadata.getUserMetadata());
        }
        if (metadata.getSSEAlgorithm() != null) {
            attributes.put("s3.sseAlgorithm", metadata.getSSEAlgorithm());
        }
        if (metadata.getVersionId() != null) {
            attributes.put("s3.version", metadata.getVersionId());
        }
    } catch (final IOException | AmazonClientException ioe) {
        getLogger().error("Failed to retrieve S3 Object for {}; routing to failure", new Object[] { flowFile, ioe });
        flowFile = session.penalize(flowFile);
        session.transfer(flowFile, REL_FAILURE);
        return;
    }
    if (!attributes.isEmpty()) {
        flowFile = session.putAllAttributes(flowFile, attributes);
    }
    session.transfer(flowFile, REL_SUCCESS);
    final long transferMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos);
    getLogger().info("Successfully retrieved S3 Object for {} in {} millis; routing to success", new Object[] { flowFile, transferMillis });
    session.getProvenanceReporter().fetch(flowFile, "http://" + bucket + ".amazonaws.com/" + key, transferMillis);
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) AmazonS3(com.amazonaws.services.s3.AmazonS3) HashMap(java.util.HashMap) AmazonClientException(com.amazonaws.AmazonClientException) IOException(java.io.IOException) S3Object(com.amazonaws.services.s3.model.S3Object) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 10 with S3Object

use of software.amazon.awssdk.services.s3.model.S3Object in project XRTB by benmfaul.

the class Configuration method processDirectory.

public void processDirectory(AmazonS3Client s3, ObjectListing listing, String bucket) throws Exception {
    for (S3ObjectSummary objectSummary : listing.getObjectSummaries()) {
        long size = objectSummary.getSize();
        System.out.println("*** Processing S3 " + objectSummary.getKey() + ", size = " + size);
        S3Object object = s3.getObject(new GetObjectRequest(bucket, objectSummary.getKey()));
        String bucketName = object.getBucketName();
        String keyName = object.getKey();
        if (keyName.contains("Darren")) {
            System.out.println("HERE");
        }
        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);
        }
    }
}
Also used : GetObjectTaggingRequest(com.amazonaws.services.s3.model.GetObjectTaggingRequest) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) S3Object(com.amazonaws.services.s3.model.S3Object) GeoTag(com.xrtb.geo.GeoTag) Tag(com.amazonaws.services.s3.model.Tag) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) GetObjectTaggingResult(com.amazonaws.services.s3.model.GetObjectTaggingResult)

Aggregations

S3Object (com.amazonaws.services.s3.model.S3Object)76 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)22 InputStream (java.io.InputStream)22 IOException (java.io.IOException)21 S3ObjectInputStream (com.amazonaws.services.s3.model.S3ObjectInputStream)16 ByteArrayInputStream (java.io.ByteArrayInputStream)15 Test (org.junit.Test)15 File (java.io.File)12 FileInputStream (java.io.FileInputStream)12 AmazonS3 (com.amazonaws.services.s3.AmazonS3)11 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)11 AmazonServiceException (com.amazonaws.AmazonServiceException)10 Date (java.util.Date)9 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)8 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)8 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)7 AmazonClientException (com.amazonaws.AmazonClientException)6 FileOutputStream (java.io.FileOutputStream)6 Test (org.testng.annotations.Test)6