Example usage for com.amazonaws ClientConfiguration setProtocol

List of usage examples for com.amazonaws ClientConfiguration setProtocol

Introduction

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

Prototype

public void setProtocol(Protocol protocol) 

Source Link

Document

Sets the protocol (i.e.

Usage

From source file:alluxio.underfs.s3a.S3AUnderFileSystem.java

License:Apache License

/**
 * Constructs a new instance of {@link S3AUnderFileSystem}.
 *
 * @param uri the {@link AlluxioURI} for this UFS
 *///from  ww  w . j av  a  2  s .c  o m
public S3AUnderFileSystem(AlluxioURI uri) {
    super(uri);
    mBucketName = uri.getHost();
    mBucketPrefix = PathUtils.normalizePath(Constants.HEADER_S3A + mBucketName, PATH_SEPARATOR);

    // Set the aws credential system properties based on Alluxio properties, if they are set
    if (Configuration.containsKey(PropertyKey.S3A_ACCESS_KEY)) {
        System.setProperty(SDKGlobalConfiguration.ACCESS_KEY_SYSTEM_PROPERTY,
                Configuration.get(PropertyKey.S3A_ACCESS_KEY));
    }
    if (Configuration.containsKey(PropertyKey.S3A_SECRET_KEY)) {
        System.setProperty(SDKGlobalConfiguration.SECRET_KEY_SYSTEM_PROPERTY,
                Configuration.get(PropertyKey.S3A_SECRET_KEY));
    }

    // Checks, in order, env variables, system properties, profile file, and instance profile
    AWSCredentialsProvider credentials = new AWSCredentialsProviderChain(
            new DefaultAWSCredentialsProviderChain());

    // Set the client configuration based on Alluxio configuration values
    ClientConfiguration clientConf = new ClientConfiguration();

    // Socket timeout
    clientConf.setSocketTimeout(Configuration.getInt(PropertyKey.UNDERFS_S3A_SOCKET_TIMEOUT_MS));

    // HTTP protocol
    if (Configuration.getBoolean(PropertyKey.UNDERFS_S3A_SECURE_HTTP_ENABLED)) {
        clientConf.setProtocol(Protocol.HTTPS);
    } else {
        clientConf.setProtocol(Protocol.HTTP);
    }

    // Proxy host
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_PROXY_HOST)) {
        clientConf.setProxyHost(Configuration.get(PropertyKey.UNDERFS_S3_PROXY_HOST));
    }

    // Proxy port
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_PROXY_PORT)) {
        clientConf.setProxyPort(Configuration.getInt(PropertyKey.UNDERFS_S3_PROXY_PORT));
    }

    mClient = new AmazonS3Client(credentials, clientConf);
    if (Configuration.containsKey(PropertyKey.UNDERFS_S3_ENDPOINT)) {
        mClient.setEndpoint(Configuration.get(PropertyKey.UNDERFS_S3_ENDPOINT));
    }
    mManager = new TransferManager(mClient);

    TransferManagerConfiguration transferConf = new TransferManagerConfiguration();
    transferConf.setMultipartCopyThreshold(MULTIPART_COPY_THRESHOLD);
    mManager.setConfiguration(transferConf);

    mAccountOwnerId = mClient.getS3AccountOwner().getId();
    // Gets the owner from user-defined static mapping from S3 canonical user id  to Alluxio
    // user name.
    String owner = CommonUtils.getValueFromStaticMapping(
            Configuration.get(PropertyKey.UNDERFS_S3_OWNER_ID_TO_USERNAME_MAPPING), mAccountOwnerId);
    // If there is no user-defined mapping, use the display name.
    if (owner == null) {
        owner = mClient.getS3AccountOwner().getDisplayName();
    }
    mAccountOwner = owner == null ? mAccountOwnerId : owner;

    AccessControlList acl = mClient.getBucketAcl(mBucketName);
    mBucketMode = S3AUtils.translateBucketAcl(acl, mAccountOwnerId);
}

From source file:ch.hesso.master.sweetcity.utils.PictureUtils.java

License:Apache License

public static Key uploadPicture(Bitmap picture, GoogleAccountCredential googleCredential) {
    if (picture == null)
        return null;

    try {//from   www  . ja va 2s.  c  o m
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);
        AmazonS3 s3Connection = new AmazonS3Client(AWS_CREDENTIALS, clientConfig);
        s3Connection.setEndpoint(ConstantsAWS.S3_END_POINT);

        ObjectMetadata pictureMetadata = new ObjectMetadata();

        String key = String.format(ConstantsAWS.S3_PICTURE_NAME_FORMAT,
                googleCredential.getSelectedAccountName(), Constants.DATE_FORMAT_IMAGE.format(new Date()));

        s3Connection.putObject(ConstantsAWS.S3_BUCKET_NAME, key, ImageUtils.bitmapToInputStream(picture),
                pictureMetadata);

        return new Key(key);
    } catch (Exception e) {
        Log.d(Constants.PROJECT_NAME, e.toString());
    }

    return null;
}

From source file:ch.hesso.master.sweetcity.utils.PictureUtils.java

License:Apache License

public static InputStream getPicture(Key key) {
    if (key == null)
        return null;

    try {/*from   w w w.  j a  v  a 2  s .  c o m*/
        ClientConfiguration clientConfig = new ClientConfiguration();
        clientConfig.setProtocol(Protocol.HTTP);
        AmazonS3 s3Connection = new AmazonS3Client(AWS_CREDENTIALS, clientConfig);
        s3Connection.setEndpoint(ConstantsAWS.S3_END_POINT);

        S3Object obj = s3Connection.getObject(ConstantsAWS.S3_BUCKET_NAME, key.toString());
        return obj.getObjectContent();
    } catch (Exception e) {
        Log.d(Constants.PROJECT_NAME, e.toString());
    }
    return null;
}

From source file:com.cloud.utils.S3Utils.java

License:Apache License

private static AmazonS3 acquireClient(final ClientOptions clientOptions) {

    final AWSCredentials credentials = new BasicAWSCredentials(clientOptions.getAccessKey(),
            clientOptions.getSecretKey());

    final ClientConfiguration configuration = new ClientConfiguration();

    if (clientOptions.isHttps() != null) {
        configuration.setProtocol(clientOptions.isHttps() == true ? HTTPS : HTTP);
    }/*from   www .j  a  v  a 2s .  co m*/

    if (clientOptions.getConnectionTimeout() != null) {
        configuration.setConnectionTimeout(clientOptions.getConnectionTimeout());
    }

    if (clientOptions.getMaxErrorRetry() != null) {
        configuration.setMaxErrorRetry(clientOptions.getMaxErrorRetry());
    }

    if (clientOptions.getSocketTimeout() != null) {
        configuration.setSocketTimeout(clientOptions.getSocketTimeout());
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format(
                "Creating S3 client with configuration: [protocol: %1$s, connectionTimeOut: "
                        + "%2$s, maxErrorRetry: %3$s, socketTimeout: %4$s]",
                configuration.getProtocol(), configuration.getConnectionTimeout(),
                configuration.getMaxErrorRetry(), configuration.getSocketTimeout()));
    }

    final AmazonS3Client client = new AmazonS3Client(credentials, configuration);

    if (isNotBlank(clientOptions.getEndPoint())) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug(format("Setting the end point for S3 client %1$s to %2$s.", client,
                    clientOptions.getEndPoint()));
        }
        client.setEndpoint(clientOptions.getEndPoint());
    }

    return client;

}

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);//  w ww. ja v  a  2  s  .  co m

    // 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);/*from w  ww.  jav a 2s.  c o m*/

    // 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.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");

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

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

    s3 = new AmazonS3Client(creds, config);

    if (endpoint != null)
        s3.setEndpoint(endpoint);//  w w w.j  av  a  2  s. c o m

    if (disableVHosts) {
        l4j.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
    if (rootKey.startsWith("/"))
        rootKey = rootKey.substring(1); // " " does not start with slash
}

From source file:com.eucalyptus.objectstorage.client.GenericS3ClientFactory.java

License:Open Source License

protected static ClientConfiguration getDefaultConfiguration(boolean withHttps) {
    ClientConfiguration config = new ClientConfiguration();
    config.setConnectionTimeout(CONNECTION_TIMEOUT_MS);
    config.setMaxConnections(DEFAULT_MAX_CONNECTIONS);
    config.setMaxErrorRetry(DEFAULT_MAX_ERROR_RETRY);
    config.setUseReaper(true);/*ww  w  .  ja v a2 s  . co m*/
    config.setSocketTimeout(DEFAULT_SOCKET_READ_TIMEOUT_MS);
    config.setProtocol(withHttps ? Protocol.HTTPS : Protocol.HTTP);
    return config;
}

From source file:com.eucalyptus.objectstorage.client.OsgInternalS3Client.java

License:Open Source License

private synchronized void initializeNewClient(@Nonnull AWSCredentials credentials, @Nonnull String endpoint,
        @Nonnull Boolean https, @Nonnull Boolean useDns) {
    ClientConfiguration config = new ClientConfiguration();
    config.setConnectionTimeout(CONNECTION_TIMEOUT_MS); //very short timeout
    config.setSocketTimeout(OSG_SOCKET_TIMEOUT_MS);
    config.setUseReaper(true);//from  w  w  w.jav  a 2s  .  c o  m
    config.setMaxConnections(OSG_MAX_CONNECTIONS);
    Protocol protocol = https ? Protocol.HTTPS : Protocol.HTTP;
    config.setProtocol(protocol);
    this.clientConfig = config;
    this.s3Client = new AmazonS3Client(credentials, config);
    this.ops = new S3ClientOptions().withPathStyleAccess(!useDns);
    this.s3Client.setS3ClientOptions(ops);
    this.instantiated = new Date();
    this.currentCredentials = credentials;
    this.setS3Endpoint(endpoint);
}

From source file:com.facebook.presto.hive.PrestoS3FileSystem.java

License:Apache License

@Override
public void initialize(URI uri, Configuration conf) throws IOException {
    checkNotNull(uri, "uri is null");
    checkNotNull(conf, "conf is null");

    this.uri = URI.create(uri.getScheme() + "://" + uri.getAuthority());
    this.workingDirectory = new Path("/").makeQualified(this.uri, new Path("/"));

    HiveClientConfig defaults = new HiveClientConfig();
    this.stagingDirectory = new File(
            conf.get(S3_STAGING_DIRECTORY, defaults.getS3StagingDirectory().toString()));
    this.maxClientRetries = conf.getInt(S3_MAX_CLIENT_RETRIES, defaults.getS3MaxClientRetries());
    int maxErrorRetries = conf.getInt(S3_MAX_ERROR_RETRIES, defaults.getS3MaxErrorRetries());
    boolean sslEnabled = conf.getBoolean(S3_SSL_ENABLED, defaults.isS3SslEnabled());
    Duration connectTimeout = Duration
            .valueOf(conf.get(S3_CONNECT_TIMEOUT, defaults.getS3ConnectTimeout().toString()));

    ClientConfiguration configuration = new ClientConfiguration();
    configuration.setMaxErrorRetry(maxErrorRetries);
    configuration.setProtocol(sslEnabled ? Protocol.HTTPS : Protocol.HTTP);
    configuration.setConnectionTimeout(Ints.checkedCast(connectTimeout.toMillis()));

    this.s3 = new AmazonS3Client(getAwsCredentials(uri, conf), configuration);
}