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:gov.usgs.cida.iplover.util.ImageStorage.java

public static AmazonS3 prepS3Client() {

    AWSCredentials credentials = null;//from www . ja  v  a2s . c o  m
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", e);
    }

    AmazonS3 s3 = new AmazonS3Client(credentials);
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    s3.setRegion(usWest2);

    return s3;
}

From source file:gr.abiss.calipso.fs.S3FilePersistenceServiceImpl.java

License:Open Source License

@PostConstruct
public void postConstruct() {
    // create S3 credentials
    BasicAWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretAccessKey);
    // setup client
    this.s3Client = new AmazonS3Client(credentials);
    LOGGER.debug("Created S3 client");
}

From source file:hudson.plugins.ec2.EC2Cloud.java

License:Open Source License

/**
 * Computes the presigned URL for the given S3 resource.
 *
 * @param path/*from ww w .ja v  a  2 s  .  c  om*/
 *      String like "/bucketName/folder/folder/abc.txt" that represents the resource to request.
 */
public URL buildPresignedURL(String path) throws AmazonClientException {
    AWSCredentials credentials = awsCredentialsProvider.getCredentials();
    long expires = System.currentTimeMillis() + 60 * 60 * 1000;
    GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(path, credentials.getAWSSecretKey());
    request.setExpiration(new Date(expires));
    AmazonS3 s3 = new AmazonS3Client(credentials);
    return s3.generatePresignedUrl(request);
}

From source file:ics.uci.edu.amazons3.S3Sample.java

License:Open Source License

public static void main(String[] args) throws IOException {
    /*/*  w w w .ja v a  2 s  .  c  om*/
     * This credentials provider implementation loads your AWS credentials
     * from a properties file at the root of your classpath.
     * 
     * Important: Be sure to fill in your AWS access credentials in the
     *            AwsCredentials.properties file before you try to run this
     *            sample.
     * http://aws.amazon.com/security-credentials
     */
    final AmazonS3 s3 = new AmazonS3Client(
            new BasicAWSCredentials("AKIAJTW5BOY6EXOGV2YQ", "PDcnFYIf9Hdo9GsKTEjLXretZ3yEg4mRCDQKjxu6"));

    String bucketName = "my-first-s3-bucket-" + UUID.randomUUID();
    String key = "MyObjectKey";

    System.out.println("===========================================");
    System.out.println("Getting Started with Amazon S3");
    System.out.println("===========================================\n");

    try {
        /*
         * Create a new S3 bucket - Amazon S3 bucket names are globally unique,
         * so once a bucket name has been taken by any user, you can't create
         * another bucket with that same name.
         *
         * You can optionally specify a location for your bucket if you want to
         * keep your data closer to your applications or users.
         */
        System.out.println("Creating bucket " + bucketName + "\n");
        s3.createBucket(bucketName);

        /*
         * List the buckets in your account
         */
        System.out.println("Listing buckets");
        for (Bucket bucket : s3.listBuckets()) {
            System.out.println(" - " + bucket.getName());
        }
        System.out.println();

        /*
         * Upload an object to your bucket - You can easily upload a file to
         * S3, or upload directly an InputStream if you know the length of
         * the data in the stream. You can also specify your own metadata
         * when uploading to S3, which allows you set a variety of options
         * like content-type and content-encoding, plus additional metadata
         * specific to your applications.
         */
        System.out.println("Uploading a new object to S3 from a file\n");
        s3.putObject(new PutObjectRequest(bucketName, key, createSampleFile()));

        /*
         * Download an object - When you download an object, you get all of
         * the object's metadata and a stream from which to read the contents.
         * It's important to read the contents of the stream as quickly as
         * possibly since the data is streamed directly from Amazon S3 and your
         * network connection will remain open until you read all the data or
         * close the input stream.
         *
         * GetObjectRequest also supports several other options, including
         * conditional downloading of objects based on modification times,
         * ETags, and selectively downloading a range of an object.
         */
        System.out.println("Downloading an object");
        S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));
        System.out.println("Content-Type: " + object.getObjectMetadata().getContentType());
        displayTextInputStream(object.getObjectContent());

        /*
         * List objects in your bucket by prefix - There are many options for
         * listing the objects in your bucket.  Keep in mind that buckets with
         * many objects might truncate their results when listing their objects,
         * so be sure to check if the returned object listing is truncated, and
         * use the AmazonS3.listNextBatchOfObjects(...) operation to retrieve
         * additional results.
         */
        System.out.println("Listing objects");
        ObjectListing objectListing = s3
                .listObjects(new ListObjectsRequest().withBucketName(bucketName).withPrefix("My"));
        for (S3ObjectSummary objectSummary : objectListing.getObjectSummaries()) {
            System.out.println(
                    " - " + objectSummary.getKey() + "  " + "(size = " + objectSummary.getSize() + ")");
        }
        System.out.println();

        /*
         * Delete an object - Unless versioning has been turned on for your bucket,
         * there is no way to undelete an object, so use caution when deleting objects.
         */
        System.out.println("Deleting an object\n");
        s3.deleteObject(bucketName, key);

        /*
         * Delete a bucket - A bucket must be completely empty before it can be
         * deleted, so remember to delete any objects from your buckets before
         * you try to delete them.
         */
        System.out.println("Deleting bucket " + bucketName + "\n");
        s3.deleteBucket(bucketName);
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which means the client encountered "
                + "a serious internal problem while trying to communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:ie.peternagy.jcrypto.module.config.S3ConfigHandler.java

License:Open Source License

@Override
public void validateConfig(Map config) {
    AWSCredentials credentials = new BasicAWSCredentials((String) config.get("access-key"),
            (String) config.get("secret-key"));
    AmazonS3 s3client = new AmazonS3Client(credentials);
    try {//from  www  . ja  va2  s.  c  o m
        System.out.println("S3 config validation...\n");
        s3client.doesBucketExist((String) config.get("bucket-name"));

        System.out.println("Valid configuration!\n");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                + "an internal error while trying to " + "communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:ie.peternagy.jcrypto.module.config.S3ConfigHandler.java

License:Open Source License

@Override
public Object getInitializedServiceClient() {
    if (service != null) {
        AWSCredentials credentials = new BasicAWSCredentials((String) service.get("access-key"),
                (String) service.get("secret-key"));
        return new AmazonS3Client(credentials);
    }/* w ww . j av a  2s  . c om*/

    return null;
}

From source file:iit.edu.supadyay.s3.S3upload.java

public static List listOfObjects(String bucketname) {
    List objectNames = new ArrayList();
    //AmazonS3 s3client = new AmazonS3Client(getCredentials());
    AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());
    System.out.println("Listing keys now");
    for (S3ObjectSummary obj : s3client.listObjects(bucketname).getObjectSummaries()) {
        System.out.println(obj.getKey());
        objectNames.add(obj.getKey());/*  www  .  ja  v a2s .  c  o  m*/
    }
    //for (ObjectListing obj : s3client.listObjects(bucketname)){

    //}
    return objectNames;
}

From source file:iit.edu.supadyay.s3.S3upload.java

public static List listOfBuckets() {
    List bucketNames = new ArrayList();
    //AWSCredentials credentials = new BasicAWSCredentials(access, secret);
    //AmazonS3 s3client = new AmazonS3Client(getCredentials());
    AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());

    for (Bucket bucket : s3client.listBuckets()) {
        bucketNames.add(bucket.getName());
        s3client.listObjects(bucket.getName());
    }//ww  w  .j a v a 2s .c o m
    //bucketNames.addAll(s3client.listBuckets());
    bucketNames.toString();
    return bucketNames;
}

From source file:iit.edu.supadyay.s3.S3upload.java

public static boolean upload(String bucketName, String uploadFileName, String keyName)
        throws IOException, InterruptedException {

    //access = "AKIAJ2YSLRUZR5B3F5HQ";
    //secret = "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";
    //AWSCredentials credentials = new BasicAWSCredentials(access, secret);
    //AmazonS3 s3client = new AmazonS3Client(getCredentials());
    AmazonS3 s3client = new AmazonS3Client(new InstanceProfileCredentialsProvider());
    try {//from www .jav  a  2 s .  c  o  m
        System.out.println("Uploading a new object to S3 from a file\n");
        File file = new File(uploadFileName);
        System.out.println("I am before here\n");
        s3client.createBucket(bucketName);
        System.out.println("I am here\n");

        s3client.putObject(new PutObjectRequest(bucketName, keyName, file));
        s3client.setObjectAcl(bucketName, keyName, CannedAccessControlList.PublicReadWrite);

    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which " + "means your request made it "
                + "to Amazon S3, but was rejected with an error response" + " for some reason.");
        System.out.println("Error Message:    " + ase.getMessage());
        System.out.println("HTTP Status Code: " + ase.getStatusCode());
        System.out.println("AWS Error Code:   " + ase.getErrorCode());
        System.out.println("Error Type:       " + ase.getErrorType());
        System.out.println("Request ID:       " + ase.getRequestId());
        return false;
    } catch (AmazonClientException ace) {
        System.out.println("Caught an AmazonClientException, which " + "means the client encountered "
                + "an internal error while trying to " + "communicate with S3, "
                + "such as not being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
        return false;
    }
    return true;
}

From source file:imperial.modaclouds.monitoring.datacollectors.monitors.DetailedCostMonitor.java

License:BSD License

@Override
public void run() {

    String accessKeyId = null;//from w ww.ja v a  2s.c  om

    String secretKey = null;

    ObjectListing objects = null;

    AmazonS3Client s3Client = null;

    String key = null;

    long startTime = 0;

    while (!dcmt.isInterrupted()) {

        if (System.currentTimeMillis() - startTime > 10000) {

            cost_nonspot = new HashMap<String, Double>();

            cost_spot = new HashMap<String, Double>();

            for (String metric : getProvidedMetrics()) {
                try {
                    VM resource = new VM(Config.getInstance().getVmType(), Config.getInstance().getVmId());
                    if (dcAgent.shouldMonitor(resource, metric)) {
                        Map<String, String> parameters = dcAgent.getParameters(resource, metric);

                        accessKeyId = parameters.get("accessKey");
                        secretKey = parameters.get("secretKey");
                        bucketName = parameters.get("bucketName");
                        filePath = parameters.get("filePath");
                        period = Integer.valueOf(parameters.get("samplingTime")) * 1000;
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                } catch (ConfigurationException e) {
                    e.printStackTrace();
                }
            }

            startTime = System.currentTimeMillis();

            AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretKey);
            s3Client = new AmazonS3Client(credentials);

            objects = s3Client.listObjects(bucketName);

            key = "aws-billing-detailed-line-items-with-resources-and-tags-";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            String date = sdf.format(new Date());
            key = key + date + ".csv.zip";

        }

        String fileName = null;
        do {
            for (S3ObjectSummary objectSummary : objects.getObjectSummaries()) {
                System.out.println(objectSummary.getKey() + "\t" + objectSummary.getSize() + "\t"
                        + StringUtils.fromDate(objectSummary.getLastModified()));
                if (objectSummary.getKey().contains(key)) {
                    fileName = objectSummary.getKey();
                    s3Client.getObject(new GetObjectRequest(bucketName, fileName),
                            new File(filePath + fileName));
                    break;
                }
            }
            objects = s3Client.listNextBatchOfObjects(objects);
        } while (objects.isTruncated());

        try {
            ZipFile zipFile = new ZipFile(filePath + fileName);
            zipFile.extractAll(filePath);
        } catch (ZipException e) {
            e.printStackTrace();
        }

        String csvFileName = fileName.replace(".zip", "");

        AnalyseFile(filePath + csvFileName);

        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            break;
        }

    }
}