Example usage for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client.

Prototype

@SdkInternalApi
AmazonS3Client(AmazonS3ClientParams s3ClientParams) 

Source Link

Document

Constructs a new client to invoke service methods on S3 using the specified parameters.

Usage

From source file:com.cleanenergyexperts.aws.cf.CloudFormationMojo.java

License:Apache License

public void execute() throws MojoExecutionException {
    getLog().info("Bucket Name: " + bucketName);
    //getLog().info("Cloud Formation Stack Name: " + stackName);

    if (artifactFile == null || !artifactFile.isFile()) {
        throw new MojoExecutionException("Cannot find artifact file to upload");
    }/*w w w. ja  v a2s . co  m*/
    String artifactKey = artifactFile.getName();
    getLog().info("Artifact Name: " + artifactKey);

    BasicAWSCredentials awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonCloudFormationClient cfClient = new AmazonCloudFormationClient(awsCredentials);
    cfClient.setEndpoint(getCloudFormationEndPoint());
    AmazonS3Client s3Client = new AmazonS3Client(awsCredentials);

    // Upload Artifact to S3
    try {
        getLog().info("Uploading artifact to S3...");
        s3Client.putObject(bucketName, artifactKey, artifactFile);
    } catch (AmazonServiceException e) {
        throw new MojoExecutionException("[SERVICE] Could Not Upload File to S3", e);
    } catch (AmazonClientException e) {
        throw new MojoExecutionException("[CLIENT] Could Not Upload File to S3", e);
    }

    // Update each stack with the new artifact file
    for (String stackName : stackNames) {
        getLog().info("Cloud Formation Stack Name: " + stackName);
        String templateBody = getTemplateBody(cfClient, stackName);
        Stack stack = getStack(cfClient, stackName);

        // If passed additional parameters, update them
        List<Parameter> parameters = stack.getParameters();
        if (stackParameters != null && !stackParameters.isEmpty()) {
            List<Parameter> tmpParams = new ArrayList<Parameter>();

            // Add Existing Parameters we haven't locally overwritten
            for (Parameter oldParam : parameters) {
                String oldKey = oldParam.getParameterKey();
                if (!stackParameters.containsKey(oldKey)) {
                    tmpParams.add(oldParam);
                }
            }

            // Add Overwrite parameters
            for (String key : stackParameters.keySet()) {
                Parameter newParam = new Parameter();
                newParam.setParameterKey(key);
                newParam.setParameterValue(stackParameters.get(key));
                tmpParams.add(newParam);
            }
            parameters = tmpParams;
        }

        // Update the Stack
        UpdateStackRequest updateStackRequest = new UpdateStackRequest();
        updateStackRequest.setStackName(stackName);
        updateStackRequest.setTemplateBody(templateBody);
        updateStackRequest.setParameters(parameters);
        updateStackRequest.setCapabilities(stack.getCapabilities());
        try {
            getLog().info("Updating Cloud Formation Stack...");
            cfClient.updateStack(updateStackRequest);
        } catch (AmazonServiceException e) {
            throw new MojoExecutionException("[SERVICE] Could Not Update Cloud Formation Stack", e);
        } catch (AmazonClientException e) {
            throw new MojoExecutionException("[CLIENT] Could Not Update Cloud Formation Stack", e);
        }
        getLog().info("Cloud Formation Stack " + stackName + "is now updating...");
    }

    getLog().info("All stacks have been updated. Complete.");
}

From source file:com.clicktravel.infrastructure.runtime.config.aws.AwsConfiguration.java

License:Apache License

@Bean
@Autowired/*from   w w w . java  2  s  .  c om*/
public AmazonS3 amazonS3Client(final AWSCredentials awsCredentials,
        @Value("${aws.s3.client.endpoint}") final String endpoint) {
    final AmazonS3 amazonS3Client = new AmazonS3Client(awsCredentials);
    logger.info("Setting AWS S3 endpoint to: " + endpoint);
    amazonS3Client.setEndpoint(endpoint);
    return amazonS3Client;
}

From source file:com.climate.oada.dao.impl.S3ResourceDAO.java

License:Open Source License

/**
 * Upload file to S3./*  w  ww . j  av  a2  s .  c o  m*/
 *
 * @param local
 *            - local file to upload.
 * @return boolean
 */
boolean uploadS3(File local) {
    boolean retval = false;
    AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
    try {
        LOG.debug("Uploading a new object to S3 from local, file name " + local.getName());
        s3client.putObject(new PutObjectRequest(bucketName, keyName, local));
        retval = true;
    } catch (AmazonServiceException ase) {
        logAWSServiceException(ase);
    } catch (AmazonClientException ace) {
        logAWSClientException(ace);
    }
    return retval;
}

From source file:com.climate.oada.dao.impl.S3ResourceDAO.java

License:Open Source License

@Override
public List<FileResource> getFileUrls(Long userId, String type) {
    List<FileResource> retval = new ArrayList<FileResource>();
    long validfor = new Long(validHours).longValue() * HOURS_TO_MILLISECONDS;
    try {//  w w  w . j  av a  2  s  .c  o  m
        AmazonS3 s3client = new AmazonS3Client(new ProfileCredentialsProvider());
        String prefix = userId.toString() + S3_SEPARATOR + type;

        LOG.debug("Listing objects from bucket " + bucketName + " with prefix " + prefix);

        ListObjectsRequest listObjectsRequest = new ListObjectsRequest().withBucketName(bucketName)
                .withPrefix(prefix);
        ObjectListing objectListing;
        do {
            objectListing = s3client.listObjects(listObjectsRequest);
            for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
                LOG.debug(" - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");

                Date expiration = new Date();
                long milliSeconds = expiration.getTime();
                milliSeconds += validfor;
                expiration.setTime(milliSeconds);

                GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(
                        bucketName, objectSummary.getKey());
                generatePresignedUrlRequest.setMethod(HttpMethod.GET);
                generatePresignedUrlRequest.setExpiration(expiration);

                FileResource res = new FileResource();
                res.setFileURL(s3client.generatePresignedUrl(generatePresignedUrlRequest));
                retval.add(res);
            }
            listObjectsRequest.setMarker(objectListing.getNextMarker());
        } while (objectListing.isTruncated());
    } catch (AmazonServiceException ase) {
        logAWSServiceException(ase);
    } catch (AmazonClientException ace) {
        logAWSClientException(ace);
    } catch (Exception e) {
        LOG.error("Unable to retrieve S3 file URLs " + e.getMessage());
    }
    return retval;
}

From source file:com.cloudbees.demo.beesshop.service.AmazonS3FileStorageService.java

License:Apache License

@Override
public void afterPropertiesSet() throws Exception {
    this.amazonS3 = new AmazonS3Client(awsCredentials);

    try {/*from   w w w.  j a v a2  s  . c  o m*/
        checkConfiguration();
    } catch (RuntimeException e) {
        logger.warn("Amazon S3 configuration problem: " + e.getMessage(), e);
    }
}

From source file:com.cloudbees.demo.beesshop.service.AmazonS3FileStorageService.java

License:Apache License

public void setAmazonCredentials(String awsAccessKey, String awsSecretKey) {
    this.awsCredentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);
    this.amazonS3 = new AmazonS3Client(awsCredentials);
}

From source file:com.cloudbees.demo.beesshop.service.AmazonS3FileStorageService.java

License:Apache License

@Required
public void setAwsCredentials(AWSCredentials awsCredentials) {
    this.awsCredentials = awsCredentials;
    this.amazonS3 = new AmazonS3Client(awsCredentials);
}

From source file:com.clouddrive.parth.AmazonOperations.java

public AmazonOperations() {
    String secretKey = "kxDFnyETb02UrLr4YT3bRjiET+/FNGUMrE3DrU4j";
    String accessKey = "AKIAII3DXT3OYD5UV4WQ";

    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
    s3 = new AmazonS3Client(awsCreds);

    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ((AmazonWebServiceClient) s3).setRegion(usWest2);
}

From source file:com.clouddrive.parth.NewClass.java

public static void init(String instanceType, String noOfinstances) throws Exception {
    INSTANCE_COUNT = Integer.parseInt(noOfinstances);
    switch (instanceType) {
    case "C1Medium":
        INSTANCE_TYPE = InstanceType.C1Medium.toString();
        break;/*from w  ww .j a v  a  2  s. c  o m*/
    case "C1Xlarge":
        INSTANCE_TYPE = InstanceType.C1Xlarge.toString();
        break;
    case "C32xlarge":
        INSTANCE_TYPE = InstanceType.C32xlarge.toString();
        break;
    case "C34xlarge":
        INSTANCE_TYPE = InstanceType.C34xlarge.toString();
        break;
    case "C38xlarge":
        INSTANCE_TYPE = InstanceType.C38xlarge.toString();
        break;
    case "C3Large":
        INSTANCE_TYPE = InstanceType.C3Large.toString();
        break;
    case "C3Xlarge":
        INSTANCE_TYPE = InstanceType.C3Xlarge.toString();
        break;
    case "Cc14xlarge":
        INSTANCE_TYPE = InstanceType.Cc14xlarge.toString();
        break;
    case "Cc28xlarge":
        INSTANCE_TYPE = InstanceType.Cc28xlarge.toString();
        break;
    case "Cg14xlarge":
        INSTANCE_TYPE = InstanceType.Cg14xlarge.toString();
        break;
    case "Cr18xlarge":
        INSTANCE_TYPE = InstanceType.Cr18xlarge.toString();
        break;
    case "G22xlarge":
        INSTANCE_TYPE = InstanceType.G22xlarge.toString();
        break;
    case "T1Micro":
        INSTANCE_TYPE = InstanceType.T1Micro.toString();
        break;

    }

    //AWSCredentials credentials = new PropertiesCredentials(NewClass.class.getClassLoader().getResourceAsStream(
    //   "AwsCredentials.properties"));
    // s3 = new AmazonS3Client(credentials);
    String secretKey = "kxDFnyETb02UrLr4YT3bRjiET+/FNGUMrE3DrU4j";
    String accessKey = "AKIAII3DXT3OYD5UV4WQ";

    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKey, secretKey);
    s3 = new AmazonS3Client(awsCreds);

    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ((AmazonWebServiceClient) s3).setRegion(usWest2);
    emr = new AmazonElasticMapReduceClient(awsCreds);
    emr.setRegion(Region.getRegion(Regions.EU_WEST_1));
}

From source file:com.cloudhub.aws.extractor.AWSCSVExtractor.java

License:Apache License

public AWSCSVExtractor(final String bucketName, final String dataFolder) {
    this.bucketName = bucketName;
    this.dataFolder = dataFolder;
    this.s3client = new AmazonS3Client(new ProfileCredentialsProvider());
}