Search in sources :

Example 11 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project nifi by apache.

the class TestPutS3Object method testSignerOverrideOptions.

@Test
public void testSignerOverrideOptions() {
    final AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    final ClientConfiguration config = new ClientConfiguration();
    final PutS3Object processor = new PutS3Object();
    final TestRunner runner = TestRunners.newTestRunner(processor);
    final List<AllowableValue> allowableSignerValues = PutS3Object.SIGNER_OVERRIDE.getAllowableValues();
    final String defaultSignerValue = PutS3Object.SIGNER_OVERRIDE.getDefaultValue();
    for (AllowableValue allowableSignerValue : allowableSignerValues) {
        String signerType = allowableSignerValue.getValue();
        if (!signerType.equals(defaultSignerValue)) {
            runner.setProperty(PutS3Object.SIGNER_OVERRIDE, signerType);
            ProcessContext context = runner.getProcessContext();
            try {
                AmazonS3Client s3Client = processor.createClient(context, credentialsProvider, config);
            } catch (IllegalArgumentException argEx) {
                Assert.fail(argEx.getMessage());
            }
        }
    }
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) AllowableValue(org.apache.nifi.components.AllowableValue) TestRunner(org.apache.nifi.util.TestRunner) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) ClientConfiguration(com.amazonaws.ClientConfiguration) ProcessContext(org.apache.nifi.processor.ProcessContext) Test(org.junit.Test)

Example 12 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project nifi by apache.

the class AbstractS3Processor method createClient.

/**
 * Create client using AWSCredentials
 *
 * @deprecated use {@link #createClient(ProcessContext, AWSCredentialsProvider, ClientConfiguration)} instead
 */
@Override
protected AmazonS3Client createClient(final ProcessContext context, final AWSCredentials credentials, final ClientConfiguration config) {
    getLogger().info("Creating client with AWS credentials");
    initializeSignerOverride(context, config);
    final AmazonS3Client s3 = new AmazonS3Client(credentials, config);
    initalizeEndpointOverride(context, s3);
    return s3;
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client)

Example 13 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project nifi by apache.

the class PutS3Object method localUploadExistsInS3.

protected boolean localUploadExistsInS3(final AmazonS3Client s3, final String bucket, final MultipartState localState) {
    ListMultipartUploadsRequest listRequest = new ListMultipartUploadsRequest(bucket);
    MultipartUploadListing listing = s3.listMultipartUploads(listRequest);
    for (MultipartUpload upload : listing.getMultipartUploads()) {
        if (upload.getUploadId().equals(localState.getUploadId())) {
            return true;
        }
    }
    return false;
}
Also used : MultipartUploadListing(com.amazonaws.services.s3.model.MultipartUploadListing) MultipartUpload(com.amazonaws.services.s3.model.MultipartUpload) ListMultipartUploadsRequest(com.amazonaws.services.s3.model.ListMultipartUploadsRequest)

Example 14 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project nifi by apache.

the class PutS3Object method getS3AgeoffListAndAgeoffLocalState.

protected MultipartUploadListing getS3AgeoffListAndAgeoffLocalState(final ProcessContext context, final AmazonS3Client s3, final long now) {
    final long ageoff_interval = context.getProperty(MULTIPART_S3_AGEOFF_INTERVAL).asTimePeriod(TimeUnit.MILLISECONDS);
    final String bucket = context.getProperty(BUCKET).evaluateAttributeExpressions().getValue();
    final Long maxAge = context.getProperty(MULTIPART_S3_MAX_AGE).asTimePeriod(TimeUnit.MILLISECONDS);
    final long ageCutoff = now - maxAge;
    final List<MultipartUpload> ageoffList = new ArrayList<>();
    if ((lastS3AgeOff.get() < now - ageoff_interval) && s3BucketLock.tryLock()) {
        try {
            ListMultipartUploadsRequest listRequest = new ListMultipartUploadsRequest(bucket);
            MultipartUploadListing listing = s3.listMultipartUploads(listRequest);
            for (MultipartUpload upload : listing.getMultipartUploads()) {
                long uploadTime = upload.getInitiated().getTime();
                if (uploadTime < ageCutoff) {
                    ageoffList.add(upload);
                }
            }
            // ageoff any local state
            ageoffLocalState(ageCutoff);
            lastS3AgeOff.set(System.currentTimeMillis());
        } catch (AmazonClientException e) {
            if (e instanceof AmazonS3Exception && ((AmazonS3Exception) e).getStatusCode() == 403 && ((AmazonS3Exception) e).getErrorCode().equals("AccessDenied")) {
                getLogger().warn("AccessDenied checking S3 Multipart Upload list for {}: {} " + "** The configured user does not have the s3:ListBucketMultipartUploads permission " + "for this bucket, S3 ageoff cannot occur without this permission.  Next ageoff check " + "time is being advanced by interval to prevent checking on every upload **", new Object[] { bucket, e.getMessage() });
                lastS3AgeOff.set(System.currentTimeMillis());
            } else {
                getLogger().error("Error checking S3 Multipart Upload list for {}: {}", new Object[] { bucket, e.getMessage() });
            }
        } finally {
            s3BucketLock.unlock();
        }
    }
    MultipartUploadListing result = new MultipartUploadListing();
    result.setBucketName(bucket);
    result.setMultipartUploads(ageoffList);
    return result;
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) MultipartUploadListing(com.amazonaws.services.s3.model.MultipartUploadListing) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception) MultipartUpload(com.amazonaws.services.s3.model.MultipartUpload) ListMultipartUploadsRequest(com.amazonaws.services.s3.model.ListMultipartUploadsRequest)

Example 15 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project nifi by apache.

the class PutS3Object method abortS3MultipartUpload.

protected void abortS3MultipartUpload(final AmazonS3Client s3, final String bucket, final MultipartUpload upload) {
    final String uploadKey = upload.getKey();
    final String uploadId = upload.getUploadId();
    final AbortMultipartUploadRequest abortRequest = new AbortMultipartUploadRequest(bucket, uploadKey, uploadId);
    try {
        s3.abortMultipartUpload(abortRequest);
        getLogger().info("Aborting out of date multipart upload, bucket {} key {} ID {}, initiated {}", new Object[] { bucket, uploadKey, uploadId, logFormat.format(upload.getInitiated()) });
    } catch (AmazonClientException ace) {
        getLogger().info("Error trying to abort multipart upload from bucket {} with key {} and ID {}: {}", new Object[] { bucket, uploadKey, uploadId, ace.getMessage() });
    }
}
Also used : AmazonClientException(com.amazonaws.AmazonClientException) AbortMultipartUploadRequest(com.amazonaws.services.s3.model.AbortMultipartUploadRequest)

Aggregations

AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)109 Test (org.junit.Test)23 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)20 AmazonClientException (com.amazonaws.AmazonClientException)18 ClientConfiguration (com.amazonaws.ClientConfiguration)18 ArrayList (java.util.ArrayList)14 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)13 HashMap (java.util.HashMap)13 AmazonS3 (com.amazonaws.services.s3.AmazonS3)12 File (java.io.File)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)12 AWSCredentials (com.amazonaws.auth.AWSCredentials)11 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)11 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)11 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)10 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)10 S3Object (com.amazonaws.services.s3.model.S3Object)9 InternalEvent (com.nextdoor.bender.InternalEvent)9 TestContext (com.nextdoor.bender.aws.TestContext)9 IOException (java.io.IOException)9