Search in sources :

Example 36 with AmazonS3

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3 in project tutorials by eugenp.

the class SpringCloudS3IntegrationTest method setupResources.

@BeforeClass
public static void setupResources() throws IOException {
    bucketName = UUID.randomUUID().toString();
    testFileToDownload = "test-file-download.txt";
    testFileToUpload = "test-file-upload.txt";
    filesWithSimilarName = new String[] { "foo/hello-apple.txt", "foo/hello-orange.txt", "bar/hello-grapes.txt" };
    similarNameFiles = new ArrayList<>();
    for (String name : filesWithSimilarName) {
        similarNameFiles.add(new File(name.substring(0, name.lastIndexOf("/") + 1)));
    }
    Files.write(Paths.get(testFileToUpload), "Hello World Uploaded!".getBytes());
    AmazonS3 amazonS3 = SpringCloudAwsTestUtil.amazonS3();
    amazonS3.createBucket(bucketName);
    amazonS3.putObject(bucketName, testFileToDownload, "Hello World");
    for (String s3Key : filesWithSimilarName) {
        amazonS3.putObject(bucketName, s3Key, "Hello World");
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) File(java.io.File) BeforeClass(org.junit.BeforeClass)

Example 37 with AmazonS3

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3 in project tutorials by eugenp.

the class SpringCloudS3IntegrationTest method cleanUpResources.

@AfterClass
public static void cleanUpResources() {
    AmazonS3 amazonS3 = SpringCloudAwsTestUtil.amazonS3();
    ListObjectsV2Result listObjectsV2Result = amazonS3.listObjectsV2(bucketName);
    for (S3ObjectSummary objectSummary : listObjectsV2Result.getObjectSummaries()) {
        amazonS3.deleteObject(bucketName, objectSummary.getKey());
    }
    amazonS3.deleteBucket(bucketName);
    new File(testFileToDownload).delete();
    new File(testFileToUpload).delete();
    similarNameFiles.forEach(File::delete);
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) ListObjectsV2Result(com.amazonaws.services.s3.model.ListObjectsV2Result) S3ObjectSummary(com.amazonaws.services.s3.model.S3ObjectSummary) File(java.io.File) AfterClass(org.junit.AfterClass)

Example 38 with AmazonS3

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3 in project opentest by mcdcorp.

the class GetS3Object method run.

@Override
public void run() {
    super.run();
    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");
    String targetFilePath = this.readStringArgument("targetFile");
    Boolean overwrite = this.readBooleanArgument("overwrite", false);
    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
    S3Object object = s3Client.getObject(new GetObjectRequest(bucket, objectKey));
    InputStream objectDataStream = object.getObjectContent();
    if (targetFilePath != null) {
        File targetFile = new File(targetFilePath);
        if (!targetFile.isAbsolute()) {
            targetFile = Paths.get(this.getActor().getTempDir().getAbsolutePath(), targetFilePath).toFile();
        }
        targetFile.getParentFile().mkdirs();
        try {
            if (overwrite) {
                Files.copy(objectDataStream, targetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            } else {
                Files.copy(objectDataStream, targetFile.toPath());
            }
        } catch (Exception ex) {
            throw new RuntimeException(String.format("Failed to transfer data from the input stream into file %s", targetFilePath), ex);
        }
        this.writeArgument("targetFile", targetFile.getAbsolutePath());
    } else {
    // TODO: Make targetFile arg optional so this branch can execute.
    // Read data in memory and write it to an output value
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) InputStream(java.io.InputStream) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) S3Object(com.amazonaws.services.s3.model.S3Object) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) File(java.io.File)

Example 39 with AmazonS3

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3 in project opentest by mcdcorp.

the class PutS3Object method run.

@Override
public void run() {
    super.run();
    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");
    String sourceFilePath = this.readStringArgument("sourceFile");
    File sourceFile = new File(sourceFilePath);
    if (sourceFile.exists()) {
        try {
            AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
            s3Client.putObject(new PutObjectRequest(bucket, objectKey, sourceFile));
        } catch (Exception ex) {
            throw new RuntimeException(String.format("Failed to upload file \"%s\" to S3 bucket \"%s\"", sourceFilePath, bucket), ex);
        }
    } else {
        throw new RuntimeException(String.format("Source file \"%s\" doesn't exist", sourceFilePath));
    }
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) File(java.io.File) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest)

Example 40 with AmazonS3

use of com.talend.shaded.com.amazonaws.services.s3.AmazonS3 in project components by Talend.

the class S3Connection method createClient.

public static AmazonS3 createClient(S3DatastoreProperties properties) {
    AWSCredentialsProviderChain credentials;
    if (properties.specifyCredentials.getValue()) {
        credentials = new AWSCredentialsProviderChain(new BasicAWSCredentialsProvider(properties.accessKey.getValue(), properties.secretKey.getValue()), new DefaultAWSCredentialsProviderChain(), new AnonymousAWSCredentialsProvider());
    } else {
        // do not be polluted by hidden accessKey/secretKey
        credentials = new AWSCredentialsProviderChain(new DefaultAWSCredentialsProviderChain(), new AnonymousAWSCredentialsProvider());
    }
    AmazonS3 conn = new AmazonS3Client(credentials);
    return conn;
}
Also used : DefaultAWSCredentialsProviderChain(com.talend.shaded.com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AmazonS3(com.talend.shaded.com.amazonaws.services.s3.AmazonS3) AmazonS3Client(com.talend.shaded.com.amazonaws.services.s3.AmazonS3Client) AnonymousAWSCredentialsProvider(com.talend.shaded.org.apache.hadoop.fs.s3a.AnonymousAWSCredentialsProvider) BasicAWSCredentialsProvider(com.talend.shaded.org.apache.hadoop.fs.s3a.BasicAWSCredentialsProvider) DefaultAWSCredentialsProviderChain(com.talend.shaded.com.amazonaws.auth.DefaultAWSCredentialsProviderChain) AWSCredentialsProviderChain(com.talend.shaded.com.amazonaws.auth.AWSCredentialsProviderChain)

Aggregations

AmazonS3 (com.amazonaws.services.s3.AmazonS3)85 AmazonServiceException (com.amazonaws.AmazonServiceException)16 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)13 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)13 S3ObjectSummary (com.amazonaws.services.s3.model.S3ObjectSummary)12 File (java.io.File)11 Test (org.junit.Test)11 S3Object (com.amazonaws.services.s3.model.S3Object)10 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)7 Bucket (com.amazonaws.services.s3.model.Bucket)7 ArrayList (java.util.ArrayList)7 AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)6 AmazonClientException (com.amazonaws.AmazonClientException)5 ClientConfiguration (com.amazonaws.ClientConfiguration)5 Regions (com.amazonaws.regions.Regions)4 AccessControlList (com.amazonaws.services.s3.model.AccessControlList)4 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)4 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Configuration (org.apache.hadoop.conf.Configuration)4