Search in sources :

Example 61 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project deeplearning4j by deeplearning4j.

the class S3Uploader method upload.

/**
     * Upload the file to the bucket.
     * Will create the bucket if it hasn't already been created
     * @param file the file to upload
     * @param bucketName the name of the bucket
     */
public void upload(File file, String bucketName) {
    AmazonS3 client = new AmazonS3Client(creds);
    bucketName = ensureValidBucketName(bucketName);
    List<Bucket> buckets = client.listBuckets();
    for (Bucket b : buckets) if (b.getName().equals(bucketName)) {
        client.putObject(bucketName, file.getName(), file);
        return;
    }
    //bucket didn't exist: create it
    client.createBucket(bucketName);
    client.putObject(bucketName, file.getName(), file);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client)

Example 62 with AmazonS3Client

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

the class S3BatchConsumerTest method createRegistry.

@Override
protected JndiRegistry createRegistry() throws Exception {
    JndiRegistry registry = super.createRegistry();
    AmazonS3ClientMock clientMock = new AmazonS3ClientMock();
    // add 6 messages, one more we will poll
    for (int counter = 0; counter < 6; 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 63 with AmazonS3Client

use of com.amazonaws.services.s3.AmazonS3Client in project jackrabbit-oak by apache.

the class S3DataStoreUtils method deleteBucket.

public static void deleteBucket(String bucket, Date date) throws Exception {
    log.info("cleaning bucket [" + bucket + "]");
    Properties props = getS3Config();
    AmazonS3Client s3service = Utils.openService(props);
    TransferManager tmx = new TransferManager(s3service);
    if (s3service.doesBucketExist(bucket)) {
        for (int i = 0; i < 4; i++) {
            tmx.abortMultipartUploads(bucket, date);
            ObjectListing prevObjectListing = s3service.listObjects(bucket);
            while (prevObjectListing != null) {
                List<DeleteObjectsRequest.KeyVersion> deleteList = new ArrayList<DeleteObjectsRequest.KeyVersion>();
                for (S3ObjectSummary s3ObjSumm : prevObjectListing.getObjectSummaries()) {
                    deleteList.add(new DeleteObjectsRequest.KeyVersion(s3ObjSumm.getKey()));
                }
                if (deleteList.size() > 0) {
                    DeleteObjectsRequest delObjsReq = new DeleteObjectsRequest(bucket);
                    delObjsReq.setKeys(deleteList);
                    s3service.deleteObjects(delObjsReq);
                }
                if (!prevObjectListing.isTruncated())
                    break;
                prevObjectListing = s3service.listNextBatchOfObjects(prevObjectListing);
            }
        }
        s3service.deleteBucket(bucket);
        log.info("bucket [ " + bucket + "] cleaned");
    } else {
        log.info("bucket [" + bucket + "] doesn't exists");
    }
    tmx.shutdownNow();
    s3service.shutdown();
}
Also used : TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ArrayList(java.util.ArrayList) ObjectListing(com.amazonaws.services.s3.model.ObjectListing) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) Properties(java.util.Properties) DeleteObjectsRequest(com.amazonaws.services.s3.model.DeleteObjectsRequest)

Example 64 with AmazonS3Client

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

the class AbstractS3Processor method initalizeEndpointOverride.

private void initalizeEndpointOverride(final ProcessContext context, final AmazonS3Client s3) {
    // if ENDPOINT_OVERRIDE is set, use PathStyleAccess
    if (StringUtils.trimToEmpty(context.getProperty(ENDPOINT_OVERRIDE).evaluateAttributeExpressions().getValue()).isEmpty() == false) {
        final S3ClientOptions s3Options = new S3ClientOptions();
        s3Options.setPathStyleAccess(true);
        s3.setS3ClientOptions(s3Options);
    }
}
Also used : S3ClientOptions(com.amazonaws.services.s3.S3ClientOptions)

Example 65 with AmazonS3Client

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

the class AbstractS3Processor method createClient.

/**
 * Create client using credentials provider. This is the preferred way for creating clients
 */
@Override
protected AmazonS3Client createClient(final ProcessContext context, final AWSCredentialsProvider credentialsProvider, final ClientConfiguration config) {
    getLogger().info("Creating client with credentials provider");
    initializeSignerOverride(context, config);
    final AmazonS3Client s3 = new AmazonS3Client(credentialsProvider, config);
    initalizeEndpointOverride(context, s3);
    return s3;
}
Also used : AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client)

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 PutObjectResult (com.amazonaws.services.s3.model.PutObjectResult)11 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)11 AWSCredentials (com.amazonaws.auth.AWSCredentials)10 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