Example usage for com.amazonaws ClientConfiguration setSignerOverride

List of usage examples for com.amazonaws ClientConfiguration setSignerOverride

Introduction

In this page you can find the example usage for com.amazonaws ClientConfiguration setSignerOverride.

Prototype

public void setSignerOverride(final String value) 

Source Link

Document

Sets the name of the signature algorithm to use for signing requests made by this client.

Usage

From source file:UploadUrlGenerator.java

License:Open Source License

public static void main(String[] args) throws Exception {
    Options opts = new Options();

    opts.addOption(Option.builder().longOpt(ENDPOINT_OPTION).required().hasArg().argName("url")
            .desc("Sets the ECS S3 endpoint to use, e.g. https://ecs.company.com:9021").build());
    opts.addOption(Option.builder().longOpt(ACCESS_KEY_OPTION).required().hasArg().argName("access-key")
            .desc("Sets the Access Key (user) to sign the request").build());
    opts.addOption(Option.builder().longOpt(SECRET_KEY_OPTION).required().hasArg().argName("secret")
            .desc("Sets the secret key to sign the request").build());
    opts.addOption(Option.builder().longOpt(BUCKET_OPTION).required().hasArg().argName("bucket-name")
            .desc("The bucket containing the object").build());
    opts.addOption(Option.builder().longOpt(KEY_OPTION).required().hasArg().argName("object-key")
            .desc("The object name (key) to access with the URL").build());
    opts.addOption(Option.builder().longOpt(EXPIRES_OPTION).hasArg().argName("minutes")
            .desc("Minutes from local time to expire the request.  1 day = 1440, 1 week=10080, "
                    + "1 month (30 days)=43200, 1 year=525600.  Defaults to 1 hour (60).")
            .build());/*  ww  w  .j a v a  2  s .com*/
    opts.addOption(Option.builder().longOpt(VERB_OPTION).hasArg().argName("http-verb").type(HttpMethod.class)
            .desc("The HTTP verb that will be used with the URL (PUT, GET, etc).  Defaults to GET.").build());
    opts.addOption(Option.builder().longOpt(CONTENT_TYPE_OPTION).hasArg().argName("mimetype")
            .desc("Sets the Content-Type header (e.g. image/jpeg) that will be used with the request.  "
                    + "Must match exactly.  Defaults to application/octet-stream for PUT/POST and "
                    + "null for all others")
            .build());

    DefaultParser dp = new DefaultParser();

    CommandLine cmd = null;
    try {
        cmd = dp.parse(opts, args);
    } catch (ParseException e) {
        System.err.println("Error: " + e.getMessage());
        HelpFormatter hf = new HelpFormatter();
        hf.printHelp("java -jar UploadUrlGenerator-xxx.jar", opts, true);
        System.exit(255);
    }

    URI endpoint = URI.create(cmd.getOptionValue(ENDPOINT_OPTION));
    String accessKey = cmd.getOptionValue(ACCESS_KEY_OPTION);
    String secretKey = cmd.getOptionValue(SECRET_KEY_OPTION);
    String bucket = cmd.getOptionValue(BUCKET_OPTION);
    String key = cmd.getOptionValue(KEY_OPTION);
    HttpMethod method = HttpMethod.GET;
    if (cmd.hasOption(VERB_OPTION)) {
        method = HttpMethod.valueOf(cmd.getOptionValue(VERB_OPTION).toUpperCase());
    }
    int expiresMinutes = 60;
    if (cmd.hasOption(EXPIRES_OPTION)) {
        expiresMinutes = Integer.parseInt(cmd.getOptionValue(EXPIRES_OPTION));
    }
    String contentType = null;
    if (method == HttpMethod.PUT || method == HttpMethod.POST) {
        contentType = "application/octet-stream";
    }

    if (cmd.hasOption(CONTENT_TYPE_OPTION)) {
        contentType = cmd.getOptionValue(CONTENT_TYPE_OPTION);
    }

    BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration cc = new ClientConfiguration();
    // Force use of v2 Signer.  ECS does not support v4 signatures yet.
    cc.setSignerOverride("S3SignerType");
    AmazonS3Client s3 = new AmazonS3Client(credentials, cc);
    s3.setEndpoint(endpoint.toString());
    S3ClientOptions s3c = new S3ClientOptions();
    s3c.setPathStyleAccess(true);
    s3.setS3ClientOptions(s3c);

    // Sign the URL
    Calendar c = Calendar.getInstance();
    c.add(Calendar.MINUTE, expiresMinutes);
    GeneratePresignedUrlRequest req = new GeneratePresignedUrlRequest(bucket, key).withExpiration(c.getTime())
            .withMethod(method);
    if (contentType != null) {
        req = req.withContentType(contentType);
    }
    URL u = s3.generatePresignedUrl(req);
    System.out.printf("URL: %s\n", u.toURI().toASCIIString());
    System.out.printf("HTTP Verb: %s\n", method);
    System.out.printf("Expires: %s\n", c.getTime());
    System.out.println("To Upload with curl:");

    StringBuilder sb = new StringBuilder();
    sb.append("curl ");

    if (method != HttpMethod.GET) {
        sb.append("-X ");
        sb.append(method.toString());
        sb.append(" ");
    }

    if (contentType != null) {
        sb.append("-H \"Content-Type: ");
        sb.append(contentType);
        sb.append("\" ");
    }

    if (method == HttpMethod.POST || method == HttpMethod.PUT) {
        sb.append("-T <filename> ");
    }

    sb.append("\"");
    sb.append(u.toURI().toASCIIString());
    sb.append("\"");

    System.out.println(sb.toString());

    System.exit(0);
}

From source file:com.emc.ecs.sync.source.S3Source.java

License:Open Source License

@Override
public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarget target) {
    Assert.hasText(accessKey, "accessKey is required");
    Assert.hasText(secretKey, "secretKey is required");
    Assert.hasText(bucketName, "bucketName is required");
    Assert.isTrue(bucketName.matches("[A-Za-z0-9._-]+"), bucketName + " is not a valid bucket name");

    AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration config = new ClientConfiguration();

    if (protocol != null)
        config.setProtocol(Protocol.valueOf(protocol.toUpperCase()));

    if (legacySignatures)
        config.setSignerOverride("S3SignerType");

    if (socketTimeoutMs >= 0)
        config.setSocketTimeout(socketTimeoutMs);

    s3 = new AmazonS3Client(creds, config);

    if (endpoint != null)
        s3.setEndpoint(endpoint);//from  w  w  w  .j a  va 2 s  .  com

    // TODO: generalize uri translation
    AwsS3Util.S3Uri s3Uri = new AwsS3Util.S3Uri();
    s3Uri.protocol = protocol;
    s3Uri.endpoint = endpoint;
    s3Uri.accessKey = accessKey;
    s3Uri.secretKey = secretKey;
    s3Uri.rootKey = rootKey;
    if (sourceUri == null)
        sourceUri = s3Uri.toUri();

    if (disableVHosts) {
        log.info(
                "The use of virtual hosted buckets on the s3 source has been DISABLED.  Path style buckets will be used.");
        S3ClientOptions opts = new S3ClientOptions();
        opts.setPathStyleAccess(true);
        s3.setS3ClientOptions(opts);
    }

    if (!s3.doesBucketExist(bucketName)) {
        throw new ConfigurationException("The bucket " + bucketName + " does not exist.");
    }

    if (rootKey == null)
        rootKey = ""; // make sure rootKey isn't null

    // for version support. TODO: genericize version support
    if (target instanceof S3Target) {
        s3Target = (S3Target) target;
        if (s3Target.isIncludeVersions()) {
            BucketVersioningConfiguration versioningConfig = s3.getBucketVersioningConfiguration(bucketName);
            List<String> versionedStates = Arrays.asList(BucketVersioningConfiguration.ENABLED,
                    BucketVersioningConfiguration.SUSPENDED);
            versioningEnabled = versionedStates.contains(versioningConfig.getStatus());
        }
    }
}

From source file:com.emc.ecs.sync.target.S3Target.java

License:Open Source License

@Override
public void configure(SyncSource source, Iterator<SyncFilter> filters, SyncTarget target) {
    Assert.hasText(accessKey, "accessKey is required");
    Assert.hasText(secretKey, "secretKey is required");
    Assert.hasText(bucketName, "bucketName is required");
    Assert.isTrue(bucketName.matches("[A-Za-z0-9._-]+"), bucketName + " is not a valid bucket name");

    AWSCredentials creds = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration config = new ClientConfiguration();

    if (protocol != null)
        config.setProtocol(Protocol.valueOf(protocol.toUpperCase()));

    if (legacySignatures)
        config.setSignerOverride("S3SignerType");

    if (socketTimeoutMs >= 0)
        config.setSocketTimeout(socketTimeoutMs);

    s3 = new AmazonS3Client(creds, config);

    if (endpoint != null)
        s3.setEndpoint(endpoint);//www  .j  a v a  2 s  .c om

    // TODO: generalize uri translation
    AwsS3Util.S3Uri s3Uri = new AwsS3Util.S3Uri();
    s3Uri.protocol = protocol;
    s3Uri.endpoint = endpoint;
    s3Uri.accessKey = accessKey;
    s3Uri.secretKey = secretKey;
    s3Uri.rootKey = rootKey;
    if (targetUri == null)
        targetUri = s3Uri.toUri();

    if (disableVHosts) {
        log.info(
                "The use of virtual hosted buckets on the s3 source has been DISABLED.  Path style buckets will be used.");
        S3ClientOptions opts = new S3ClientOptions();
        opts.setPathStyleAccess(true);
        s3.setS3ClientOptions(opts);
    }

    // for version support. TODO: genericize version support
    if (source instanceof S3Source) {
        s3Source = (S3Source) source;
        if (!s3Source.isVersioningEnabled())
            includeVersions = false; // don't include versions if source versioning is off
    } else if (includeVersions) {
        throw new ConfigurationException(
                "Object versions are currently only supported with the S3 source & target plugins.");
    }

    if (!s3.doesBucketExist(bucketName)) {
        if (createBucket) {
            s3.createBucket(bucketName);
            if (includeVersions)
                s3.setBucketVersioningConfiguration(new SetBucketVersioningConfigurationRequest(bucketName,
                        new BucketVersioningConfiguration(BucketVersioningConfiguration.ENABLED)));
        } else {
            throw new ConfigurationException("The bucket " + bucketName + " does not exist.");
        }
    }

    if (rootKey == null)
        rootKey = ""; // make sure rootKey isn't null

    if (includeVersions) {
        String status = s3.getBucketVersioningConfiguration(bucketName).getStatus();
        if (BucketVersioningConfiguration.OFF.equals(status))
            throw new ConfigurationException("The specified bucket does not have versioning enabled.");
    }

    if (mpuThresholdMB > AwsS3Util.MAX_PUT_SIZE_MB) {
        log.warn("{}MB is above the maximum PUT size of {}MB. the maximum will be used instead", mpuThresholdMB,
                AwsS3Util.MAX_PUT_SIZE_MB);
        mpuThresholdMB = AwsS3Util.MAX_PUT_SIZE_MB;
    }
    if (mpuPartSizeMB < AwsS3Util.MIN_PART_SIZE_MB) {
        log.warn("{}MB is below the minimum MPU part size of {}MB. the minimum will be used instead",
                mpuPartSizeMB, AwsS3Util.MIN_PART_SIZE_MB);
        mpuPartSizeMB = AwsS3Util.MIN_PART_SIZE_MB;
    }
}

From source file:com.emc.vipr.s3.sample.AWSS3Factory.java

License:Open Source License

public static AmazonS3 getS3Client() {
    BasicAWSCredentials creds = new BasicAWSCredentials(S3_ACCESS_KEY_ID, S3_SECRET_KEY);

    ClientConfiguration cc = new ClientConfiguration();
    //cc.setProxyHost("localhost");
    //cc.setProxyPort(8888);

    // Force use of v2 Signer.
    cc.setSignerOverride("S3SignerType");

    AmazonS3 client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(creds))
            .withClientConfiguration(cc).withPathStyleAccessEnabled(true) // Path-style bucket naming is highly recommended
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(S3_ENDPOINT, null)).build();

    return client;
}

From source file:com.ge.predix.sample.blobstore.connector.spring.BlobstoreServiceConnectorCreator.java

License:Apache License

/**
 * Creates the BlobStore context using S3Client
 *
 * @param serviceInfo Object Store Service Info Object
 * @param serviceConnectorConfig Cloud Foundry Service Connector Configuration
 *
 * @return BlobstoreService Instance of the ObjectStore Service
 *///w  w w .  java  2  s.  c  om
@Override
public BlobstoreService create(BlobstoreServiceInfo serviceInfo,
        ServiceConnectorConfig serviceConnectorConfig) {
    log.info("create() invoked with serviceInfo? = " + (serviceInfo == null));
    ClientConfiguration config = new ClientConfiguration();
    config.setProtocol(Protocol.HTTPS);

    S3ClientOptions options = new S3ClientOptions();
    config.setSignerOverride("S3SignerType");

    BasicAWSCredentials creds = new BasicAWSCredentials(serviceInfo.getObjectStoreAccessKey(),
            serviceInfo.getObjectStoreSecretKey());
    AmazonS3Client s3Client = new AmazonS3Client(creds, config);
    s3Client.setEndpoint(serviceInfo.getUrl());
    s3Client.setS3ClientOptions(options);

    try {
        // Remove the Credentials from the Object Store URL
        URL url = new URL(serviceInfo.getUrl());
        String urlWithoutCredentials = url.getProtocol() + "://" + url.getHost();

        // Return BlobstoreService
        return new BlobstoreService(s3Client, serviceInfo.getBucket(), urlWithoutCredentials);
    } catch (MalformedURLException e) {
        log.error("create(): Couldnt parse the URL provided by VCAP_SERVICES. Exception = " + e.getMessage());
        throw new RuntimeException("Blobstore URL is Invalid", e);
    }
}

From source file:com.ibm.stocator.fs.cos.COSAPIClient.java

License:Apache License

/**
 * Initializes connection management//ww w  .j a  va 2  s  .  c o  m
 *
 * @param conf Hadoop configuration
 * @param clientConf client SDK configuration
 */
private void initConnectionSettings(Configuration conf, ClientConfiguration clientConf) throws IOException {
    clientConf.setMaxConnections(
            Utils.getInt(conf, FS_COS, FS_ALT_KEYS, MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS));
    clientConf.setClientExecutionTimeout(
            Utils.getInt(conf, FS_COS, FS_ALT_KEYS, CLIENT_EXEC_TIMEOUT, DEFAULT_CLIENT_EXEC_TIMEOUT));
    clientConf.setMaxErrorRetry(
            Utils.getInt(conf, FS_COS, FS_ALT_KEYS, MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES));
    clientConf.setConnectionTimeout(
            Utils.getInt(conf, FS_COS, FS_ALT_KEYS, ESTABLISH_TIMEOUT, DEFAULT_ESTABLISH_TIMEOUT));
    clientConf
            .setSocketTimeout(Utils.getInt(conf, FS_COS, FS_ALT_KEYS, SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT));
    clientConf.setRequestTimeout(
            Utils.getInt(conf, FS_COS, FS_ALT_KEYS, REQUEST_TIMEOUT, DEFAULT_REQUEST_TIMEOUT));
    int sockSendBuffer = Utils.getInt(conf, FS_COS, FS_ALT_KEYS, SOCKET_SEND_BUFFER,
            DEFAULT_SOCKET_SEND_BUFFER);
    int sockRecvBuffer = Utils.getInt(conf, FS_COS, FS_ALT_KEYS, SOCKET_RECV_BUFFER,
            DEFAULT_SOCKET_RECV_BUFFER);
    clientConf.setSocketBufferSizeHints(sockSendBuffer, sockRecvBuffer);
    String signerOverride = Utils.getTrimmed(conf, FS_COS, FS_ALT_KEYS, SIGNING_ALGORITHM, "");
    if (!signerOverride.isEmpty()) {
        LOG.debug("Signer override = {}", signerOverride);
        clientConf.setSignerOverride(signerOverride);
    }

    String userAgentPrefix = Utils.getTrimmed(conf, FS_COS, FS_ALT_KEYS, USER_AGENT_PREFIX,
            DEFAULT_USER_AGENT_PREFIX);
    String userAgentName = singletoneInitTimeData.getUserAgentName();
    if (!userAgentPrefix.equals(DEFAULT_USER_AGENT_PREFIX)) {
        userAgentName = userAgentPrefix + " " + userAgentName;
    }
    clientConf.setUserAgentPrefix(userAgentName);
}

From source file:com.upplication.s3fs.S3FileSystemProvider.java

License:Open Source License

protected ClientConfiguration createClientConfig(Properties props) {
    ClientConfiguration config = new ClientConfiguration();

    if (props == null)
        return config;

    if (props.containsKey("connection_timeout")) {
        log.trace("AWS client config - connection_timeout: {}", props.getProperty("connection_timeout"));
        config.setConnectionTimeout(Integer.parseInt(props.getProperty("connection_timeout")));
    }/*from  ww w . j a  v a2s.  com*/

    if (props.containsKey("max_connections")) {
        log.trace("AWS client config - max_connections: {}", props.getProperty("max_connections"));
        config.setMaxConnections(Integer.parseInt(props.getProperty("max_connections")));
    }

    if (props.containsKey("max_error_retry")) {
        log.trace("AWS client config - max_error_retry: {}", props.getProperty("max_error_retry"));
        config.setMaxErrorRetry(Integer.parseInt(props.getProperty("max_error_retry")));
    }

    if (props.containsKey("protocol")) {
        log.trace("AWS client config - protocol: {}", props.getProperty("protocol"));
        config.setProtocol(Protocol.valueOf(props.getProperty("protocol").toUpperCase()));
    }

    if (props.containsKey("proxy_domain")) {
        log.trace("AWS client config - proxy_domain: {}", props.getProperty("proxy_domain"));
        config.setProxyDomain(props.getProperty("proxy_domain"));
    }

    if (props.containsKey("proxy_host")) {
        log.trace("AWS client config - proxy_host: {}", props.getProperty("proxy_host"));
        config.setProxyHost(props.getProperty("proxy_host"));
    }

    if (props.containsKey("proxy_port")) {
        log.trace("AWS client config - proxy_port: {}", props.getProperty("proxy_port"));
        config.setProxyPort(Integer.parseInt(props.getProperty("proxy_port")));
    }

    if (props.containsKey("proxy_username")) {
        log.trace("AWS client config - proxy_username: {}", props.getProperty("proxy_username"));
        config.setProxyUsername(props.getProperty("proxy_username"));
    }

    if (props.containsKey("proxy_password")) {
        log.trace("AWS client config - proxy_password: {}", props.getProperty("proxy_password"));
        config.setProxyPassword(props.getProperty("proxy_password"));
    }

    if (props.containsKey("proxy_workstation")) {
        log.trace("AWS client config - proxy_workstation: {}", props.getProperty("proxy_workstation"));
        config.setProxyWorkstation(props.getProperty("proxy_workstation"));
    }

    if (props.containsKey("signer_override")) {
        log.debug("AWS client config - signerOverride: {}", props.getProperty("signer_override"));
        config.setSignerOverride(props.getProperty("signer_override"));
    }

    if (props.containsKey("socket_send_buffer_size_hints")
            || props.containsKey("socket_recv_buffer_size_hints")) {
        log.trace("AWS client config - socket_send_buffer_size_hints: {}, socket_recv_buffer_size_hints: {}",
                props.getProperty("socket_send_buffer_size_hints", "0"),
                props.getProperty("socket_recv_buffer_size_hints", "0"));
        int send = Integer.parseInt(props.getProperty("socket_send_buffer_size_hints", "0"));
        int recv = Integer.parseInt(props.getProperty("socket_recv_buffer_size_hints", "0"));
        config.setSocketBufferSizeHints(send, recv);
    }

    if (props.containsKey("socket_timeout")) {
        log.trace("AWS client config - socket_timeout: {}", props.getProperty("socket_timeout"));
        config.setSocketTimeout(Integer.parseInt(props.getProperty("socket_timeout")));
    }

    if (props.containsKey("user_agent")) {
        log.trace("AWS client config - user_agent: {}", props.getProperty("user_agent"));
        config.setUserAgent(props.getProperty("user_agent"));
    }

    return config;
}

From source file:grails.plugins.crm.content.aws.AmazonS3ClientFactory.java

License:Apache License

@Override
public AmazonS3 getObject() {
    AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    ClientConfiguration clientConfiguration = new ClientConfiguration();
    clientConfiguration.setSignerOverride("AWSS3V4SignerType");

    return AmazonS3ClientBuilder.standard()
            .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(endpoint, region))
            .withPathStyleAccessEnabled(true).withClientConfiguration(clientConfiguration)
            .withCredentials(new AWSStaticCredentialsProvider(credentials)).build();
}

From source file:org.apache.nifi.processors.aws.s3.AbstractS3Processor.java

License:Apache License

private void initializeSignerOverride(final ProcessContext context, final ClientConfiguration config) {
    String signer = context.getProperty(SIGNER_OVERRIDE).getValue();

    if (signer != null && !signer.equals(SIGNER_OVERRIDE.getDefaultValue())) {
        config.setSignerOverride(signer);
    }//from w  ww  . j a va  2s  .  c  o  m
}

From source file:org.apache.zeppelin.notebook.repo.OldS3NotebookRepo.java

License:Apache License

/**
 * Create AWS client configuration and return it.
 * @return AWS client configuration/*from ww  w.java  2 s.co  m*/
 */
private ClientConfiguration createClientConfiguration() {
    ClientConfigurationFactory configFactory = new ClientConfigurationFactory();
    ClientConfiguration config = configFactory.getConfig();

    String s3SignerOverride = conf.getS3SignerOverride();
    if (StringUtils.isNotBlank(s3SignerOverride)) {
        config.setSignerOverride(s3SignerOverride);
    }

    return config;
}