Example usage for com.amazonaws.services.securitytoken.model GetSessionTokenRequest GetSessionTokenRequest

List of usage examples for com.amazonaws.services.securitytoken.model GetSessionTokenRequest GetSessionTokenRequest

Introduction

In this page you can find the example usage for com.amazonaws.services.securitytoken.model GetSessionTokenRequest GetSessionTokenRequest.

Prototype

GetSessionTokenRequest

Source Link

Usage

From source file:com.ipcglobal.fredimportaws.TsvsToRedshift.java

License:Apache License

/**
 * Copy s3 files to redshift table./*from  w w w.  ja v a 2 s.c  o m*/
 *
 * @throws Exception the exception
 */
private void copyS3FilesToRedshiftTable() throws Exception {
    GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest();
    GetSessionTokenResult getSessionTokenResult = stsClient.getSessionToken(getSessionTokenRequest);
    Credentials credentialsToken = getSessionTokenResult.getCredentials();
    String jdbcRedshiftUrl = properties.getProperty("jdbcRedshiftUrl");
    String jdbcRedshiftDriverClass = properties.getProperty("jdbcRedshiftDriverClass");
    String jdbcRedshiftLogin = properties.getProperty("jdbcRedshiftLogin");
    String jdbcRedshiftPassword = properties.getProperty("jdbcRedshiftPassword");

    Class.forName(jdbcRedshiftDriverClass);
    Connection con = null;
    Statement statement = null;

    try {
        String tableName = properties.getProperty("tableNameFred").trim();
        con = DriverManager.getConnection(jdbcRedshiftUrl, jdbcRedshiftLogin, jdbcRedshiftPassword);
        statement = con.createStatement();
        createDatabase(statement); // just in case...
        // Drop/Create table (more efficient than deleting all of the rows)
        dropTable(statement, tableName);
        statement.execute(createTableStatement(tableName));

        long beforeCopy = System.currentTimeMillis();
        String s3SourceBucketPrefix = "s3://" + awsBucketName + "/" + awsBucketTsvPrefix + "/";
        String s3Copy = "copy " + tableName + " from '" + s3SourceBucketPrefix + "' "
                + "CREDENTIALS 'aws_access_key_id=" + credentialsToken.getAccessKeyId().replace("\\", "\\\\")
                + ";" + "aws_secret_access_key=" + credentialsToken.getSecretAccessKey().replace("\\", "\\\\")
                + ";" + "token=" + credentialsToken.getSessionToken().replace("\\", "\\\\") + "' "
                + "delimiter '\\t' gzip";
        statement.executeUpdate(s3Copy);

    } catch (Exception e) {
        log.error(e);
        throw e;
    } finally {
        try {
            if (statement != null && !statement.isClosed())
                statement.close();
        } catch (Exception e) {
            log.warn("Exception closing statement: " + e.getMessage());
        }

        try {
            if (con != null && !con.isClosed())
                con.close();
        } catch (Exception e) {
            log.warn("Exception closing connection: " + e.getMessage());
        }
    }
}

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

/**
 *
 * @return/*from  ww  w . j  av  a2 s  .co m*/
 */
public static AWSCredentials getCredentials() {
    AWSSecurityTokenServiceClient stsClient = new AWSSecurityTokenServiceClient(
            new ProfileCredentialsProvider());

    //
    // Manually start a session.
    GetSessionTokenRequest getSessionTokenRequest = new GetSessionTokenRequest();
    // Following duration can be set only if temporary credentials are requested by an IAM user.
    getSessionTokenRequest.setDurationSeconds(7200);

    GetSessionTokenResult sessionTokenResult = stsClient.getSessionToken(getSessionTokenRequest);
    Credentials sessionCredentials = sessionTokenResult.getCredentials();

    // Package the temporary security credentials as 
    // a BasicSessionCredentials object, for an Amazon S3 client object to use.
    BasicSessionCredentials basicSessionCredentials = new BasicSessionCredentials(
            sessionCredentials.getAccessKeyId(), sessionCredentials.getSecretAccessKey(),
            sessionCredentials.getSessionToken());

    return basicSessionCredentials;

}