Example usage for com.amazonaws.auth PropertiesCredentials PropertiesCredentials

List of usage examples for com.amazonaws.auth PropertiesCredentials PropertiesCredentials

Introduction

In this page you can find the example usage for com.amazonaws.auth PropertiesCredentials PropertiesCredentials.

Prototype

public PropertiesCredentials(InputStream inputStream) throws IOException 

Source Link

Document

Reads the specified input stream as a stream of Java properties file content and extracts the AWS access key ID and secret access key from the properties.

Usage

From source file:com.cloudkon.remote.worker.Dynamodb.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.ProfilesConfigFile
 * @see com.amazonaws.ClientConfiguration
 *//*from  w  ww. j  a v  a 2 s  . c  o m*/
public static void init() {
    /*
     * Load the credentials
     */
    AWSCredentials credentials = null;
    try {
        //InputStream credentialsFile = Dynamodb.class.getResourceAsStream("awsSecuCredentials.properties");
        InputStream credentialsFile = new FileInputStream("./awsSecuCredentials.properties");
        credentials = new PropertiesCredentials(credentialsFile);
    } catch (Exception e) {
        throw new AmazonClientException("Cannot load the credentials from the credential file. ", e);
    }
    dynamoDB = new AmazonDynamoDBClient(credentials);
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    dynamoDB.setRegion(usEast1);
}

From source file:com.dev.appx.sns.SNSMobilePush.java

License:Open Source License

public static void igniteSNS() throws IOException {
    /*//from   www  .j  a  v a  2 s.com
     * TODO: 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
     */
    AmazonSNS sns = new AmazonSNSClient(
            new PropertiesCredentials(SNSMobilePush.class.getResourceAsStream("/AwsCredentials.properties")));

    sns.setEndpoint("https://sns.us-west-2.amazonaws.com");
    System.out.println("===========================================\n");
    System.out.println("Getting Started with Amazon SNS");
    System.out.println("===========================================\n");
    try {
        SNSMobilePush sample = new SNSMobilePush(sns);
        /* TODO: Uncomment the services you wish to use. */
        //sample.demoAndroidAppNotification();
        // sample.demoKindleAppNotification();
        // sample.demoAppleAppNotification();
        // sample.demoAppleSandboxAppNotification();
        // sample.demoBaiduAppNotification();
        // sample.demoWNSAppNotification();
        // sample.demoMPNSAppNotification();
        sample.createTopic("test");
    } catch (AmazonServiceException ase) {
        System.out.println("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon SNS, 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 SNS, such as not "
                + "being able to access the network.");
        System.out.println("Error Message: " + ace.getMessage());
    }
}

From source file:com.dev.appx.sns.SNSMobilePush.java

License:Open Source License

public void deleteTopic(String topicArn) throws IOException {

    //AmazonSNSClient snsClient = new AmazonSNSClient(new ClasspathPropertiesFileCredentialsProvider());   

    AmazonSNS snsClient = new AmazonSNSClient(
            new PropertiesCredentials(SNSMobilePush.class.getResourceAsStream("AwsCredentials.properties")));
    //delete an SNS topic
    DeleteTopicRequest deleteTopicRequest = new DeleteTopicRequest(topicArn);
    snsClient.deleteTopic(deleteTopicRequest);
    //get request id for DeleteTopicRequest from SNS metadata
    System.out.println("DeleteTopicRequest - " + snsClient.getCachedResponseMetadata(deleteTopicRequest));

}

From source file:com.dtolabs.rundeck.ec2.NodeGenerator.java

License:Apache License

public static void main(final String[] args) throws IOException, GeneratorException {
    File outfile = null;/*from www  . j av a 2  s .c  om*/

    //load generator mapping
    if (args.length < 2) {
        System.err.println(
                "usage: <credentials.properties> <endpoint> [mapping.properties] [outfile] [query parameters, \"a=b\" ...]");
        System.err.println(
                "\t optional arguments can be replaced by \"-\" to use the default, and then query parameters appended");
        System.exit(2);
    }

    final InputStream stream = new FileInputStream(args[0]);
    final String endPoint = args[1];
    final AWSCredentials credentials = new PropertiesCredentials(stream);

    Properties mapping = new Properties();
    if (args.length > 2 && !"-".equals(args[2])) {
        mapping.load(new FileInputStream(args[2]));
    } else {
        mapping.load(NodeGenerator.class.getClassLoader().getResourceAsStream("simplemapping.properties"));
    }

    final ResourceXMLGenerator gen;
    if (args.length > 3 && !"-".equals(args[3])) {
        outfile = new File(args[3]);
        gen = new ResourceXMLGenerator(outfile);
    } else {
        //use stdout
        gen = new ResourceXMLGenerator(System.out);
    }
    ArrayList<String> params = new ArrayList<String>();
    if (args.length > 4) {
        for (int i = 4; i < args.length; i++) {
            params.add(args[i]);
        }
    }

    Set<Instance> instances = performQuery(credentials, endPoint, params);

    for (final Instance inst : instances) {
        final INodeEntry iNodeEntry = instanceToNode(inst, mapping);
        if (null != iNodeEntry) {
            gen.addNode(iNodeEntry);
        }
    }
    gen.generate();
    //
    if (null != outfile) {
        System.out.println("XML Stored: " + outfile.getAbsolutePath());
    }
}

From source file:com.flipzu.PostProcThread.java

License:Apache License

private boolean uploadToS3(Broadcast bcast, boolean delete) {
    debug.logPostProc("PostProcThread, S3 upload for " + bcast.getFilename());

    if (bcast.getFilename() == null) {
        debug.logPostProc("PostProcThread, uploadToS3, filename is null");
        return false;
    }/*w  w w .j  ava2  s  .  c  om*/

    File file = new File(bcast.getFilename());
    if (!file.exists()) {
        debug.logPostProc("PostProcThread, uploadToS3, " + bcast.getFilename() + " does not exist");
        return false;
    }

    AmazonS3 s3 = null;

    try {
        InputStream is = new FileInputStream("aws.properties");
        s3 = new AmazonS3Client(new PropertiesCredentials(is));
    } catch (Exception e) {
        Debug.getInstance().logError("uploadToS3 Error ", e);
        return false;
    }

    String bucketName = Config.getInstance().getS3Bucket();
    String dirName = Config.getInstance().getS3dir();
    String objName = dirName + "/" + bcast.getId() + Config.getInstance().getFileWriterExtension();

    PutObjectRequest po = new PutObjectRequest(bucketName, objName, file);

    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentType("audio/mpeg");
    po.setMetadata(metadata);
    po.setCannedAcl(CannedAccessControlList.PublicRead);

    try {
        s3.putObject(po);
    } catch (AmazonServiceException ase) {
        debug.logPostProc("Caught an AmazonServiceException, which means your request made it "
                + "to Amazon S3, but was rejected with an error response for some reason.");
        debug.logPostProc("Error Message:    " + ase.getMessage());
        debug.logPostProc("HTTP Status Code: " + ase.getStatusCode());
        debug.logPostProc("AWS Error Code:   " + ase.getErrorCode());
        debug.logPostProc("Error Type:       " + ase.getErrorType());
        debug.logPostProc("Request ID:       " + ase.getRequestId());
        return false;

    } catch (AmazonClientException ace) {
        debug.logPostProc("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.");
        debug.logPostProc("Error Message: " + ace.getMessage());
        return false;
    }

    if (delete) {
        if (Config.getInstance().deleteSmallBcasts())
            /* check and remove empty/short broadcasts */
            cleanCrappyBroadcasts(bcast.getKey(), file);

        debug.logPostProc("uploadToS3, deleting file " + bcast.getFilename());
        file.delete();
    }

    return true;

}

From source file:com.flipzu.PostProcThread.java

License:Apache License

private void consolidateS3(Broadcast bcast) {
    debug.logPostProc("PostProcThread, consolidate S3 for " + bcast);

    File file = new File(bcast.getFilename());
    if (!file.exists()) {
        debug.logPostProc("consolidateS3, empty broadcast, doing nothing");
        return;//from   w  w w  .j av  a  2 s .c om
    }

    AmazonS3 s3 = null;

    try {
        InputStream is = new FileInputStream("aws.properties");
        s3 = new AmazonS3Client(new PropertiesCredentials(is));
    } catch (Exception e) {
        debug.logError("consolidateS3 Error ", e);
        return;
    }

    String bucketName = Config.getInstance().getS3Bucket();
    String dirName = Config.getInstance().getS3dir();
    String objName = dirName + "/" + bcast.getId() + Config.getInstance().getFileWriterExtension();

    S3Object obj = null;
    try {
        obj = s3.getObject(bucketName, objName);
    } catch (AmazonServiceException ase) {
        debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing...");
        return;
    } catch (AmazonClientException ace) {
        debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing...");
        return;
    }

    if (obj == null) {
        debug.logPostProc("consolidateS3 for " + bcast + ". File not found, doing nothing.");
        return;
    }

    debug.logPostProc("consolidateS3 for " + bcast + ". File found, consolidating.");

    String auxFile = Config.getInstance().getFileWriterDestDir() + "/" + bcast.getId() + "-aux"
            + Config.getInstance().getFileWriterExtension();

    BufferedOutputStream bosAux = null;
    try {
        FileOutputStream fos = new FileOutputStream(auxFile);
        bosAux = new BufferedOutputStream(fos);
    } catch (FileNotFoundException e) {
        debug.logError("consolidateS3 for, error creating output stream", e);
        return;
    }

    BufferedInputStream is = new BufferedInputStream(obj.getObjectContent());

    /* fetch file from S3 */
    int r = 0;
    do {
        byte[] b = new byte[1024];
        try {
            r = is.read(b);
            if (r > 0)
                bosAux.write(b, 0, r);
        } catch (IOException e) {
            debug.logError("consolidateS3 error", e);
            /* cleanup */
            File aFile = new File(auxFile);
            aFile.delete();
            return;
        }
    } while (r > 0);

    try {
        is.close();
    } catch (IOException e) {
        debug.logError("consolidateS3 error", e);
    }

    /* append our file to aux file */
    BufferedInputStream bis;
    try {
        FileInputStream fis = new FileInputStream(bcast.getFilename());
        bis = new BufferedInputStream(fis);
    } catch (FileNotFoundException e) {
        debug.logPostProc("consolidateS3 error, FileNotFoundException");
        return;
    }

    r = 0;
    do {
        byte[] b = new byte[1024];
        try {
            r = bis.read(b);
            bosAux.write(b);
        } catch (IOException e) {
            debug.logError("consolidateS3 error", e);
            return;
        }
    } while (r > 0);

    try {
        bis.close();
        bosAux.close();
    } catch (IOException e) {
        debug.logError("consolidateS3 error", e);
    }

    /* delete old crap */
    file.delete();

    bcast.setFilename(auxFile);

    debug.logPostProc("consolidateS3 for " + bcast + ". File consolidated in " + bcast.getFilename());

    return;
}

From source file:com.github.trask.sandbox.ec2.Ec2Service.java

License:Apache License

public Ec2Service(File awsCredentialsFile) throws FileNotFoundException, IllegalArgumentException, IOException {

    AWSCredentials credentials = new PropertiesCredentials(awsCredentialsFile);
    ec2 = new AmazonEC2Client(credentials);
    iam = new AmazonIdentityManagementClient(credentials);
}

From source file:com.hazelcast.simulator.provisioner.AwsProvisionerCli.java

License:Open Source License

static AwsProvisioner init(String[] args) {
    AwsProvisionerCli cli = new AwsProvisionerCli();
    OptionSet options = initOptionsWithHelp(cli.parser, args);

    SimulatorProperties properties = loadSimulatorProperties(options, cli.propertiesFileSpec);
    ComponentRegistry componentRegistry = loadComponentRegister(new File(AgentsFile.NAME), false);

    try {/*from  w w w .j  a v  a  2  s .  c  o m*/
        String awsCredentialsPath = properties.get("AWS_CREDENTIALS", "awscredentials.properties");
        File credentialsFile = new File(awsCredentialsPath);
        AWSCredentials credentials = new PropertiesCredentials(credentialsFile);
        AmazonEC2 ec2 = new AmazonEC2Client(credentials);
        AmazonElasticLoadBalancingClient elb = new AmazonElasticLoadBalancingClient(credentials);

        return new AwsProvisioner(ec2, elb, componentRegistry, properties);
    } catch (Exception e) {
        throw new CommandLineExitException("Credentials file could not be loaded", e);
    }
}

From source file:com.hpcloud.daas.ec2.AwsConsoleApp.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 *//*from w  w  w . j av  a  2 s  .  c  om*/
private static void init() throws Exception {
    try {
        AWSCredentials credentials = new PropertiesCredentials(
                AwsConsoleApp.class.getResourceAsStream("AwsCredentials.properties"));

        ec2 = new AmazonEC2Client(credentials);
        // set our endpoint to HP Cloud
        ec2.setEndpoint("https://az-2.region-a.geo-1.ec2-compute.hpcloudsvc.com/services/Cloud/");

    } catch (Exception e) {
        System.out.println(e);
    }
}

From source file:com.leverno.ysbos.archive.example.ArchiveDownloadHighLevel.java

License:Open Source License

public static AWSCredentials getCredentials() {
    AWSCredentials credentials = null;//  w  w  w.  j  ava 2 s.c o m

    try {
        credentials = new PropertiesCredentials(new File(Constants.propertiesPath));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return credentials;
}