Search in sources :

Example 36 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project bayou by capergroup.

the class S3LoggerBase method putToS3.

/**
 * Performs an S3 Put Object operation storing the UTF-8 bytes of logMsg under the given key
 * using construction provided AWS credentials.
 *
 * @param objectKey the S3 object key. may not be null or whitespace only.
 * @param logMsg the message to store
 * @throws IllegalArgumentException if objectKey is whitespace only.
 */
void putToS3(String objectKey, String logMsg) {
    if (objectKey == null)
        throw new NullPointerException("objectKey");
    if (objectKey.trim().length() == 0)
        throw new IllegalArgumentException("objectKey may not be only whitespace.");
    /*
         * Make the client used to send the log msg to S3.
         */
    AmazonS3 client;
    {
        Regions regions = Regions.US_EAST_1;
        if (_credentials == null) {
            client = // get creds from environment
            AmazonS3ClientBuilder.standard().withRegion(regions).build();
        } else {
            client = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(_credentials)).withRegion(regions).build();
        }
    }
    /*
         * Store the log msg in S3.
         */
    byte[] logMsgBytes = logMsg.getBytes(StandardCharsets.UTF_8);
    ObjectMetadata metadata = new ObjectMetadata();
    metadata.setContentLength(logMsgBytes.length);
    client.putObject(_bucketName, objectKey, new ByteArrayInputStream(logMsgBytes), metadata);
    _logger.debug("exiting");
}
Also used : AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) ByteArrayInputStream(java.io.ByteArrayInputStream) Regions(com.amazonaws.regions.Regions) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata)

Example 37 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project georocket by georocket.

the class S3Store method getS3Client.

/**
 * Get or initialize the S3 client.
 * Note: this method must be synchronized because we're accessing the
 * {@link #s3Client} field and we're calling this method from a worker thread.
 * @return the S3 client
 */
private synchronized AmazonS3 getS3Client() {
    if (s3Client == null) {
        BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard().withCredentials(new AWSStaticCredentialsProvider(credentials));
        if (forceSignatureV2) {
            ClientConfigurationFactory configFactory = new ClientConfigurationFactory();
            ClientConfiguration config = configFactory.getConfig();
            config.setSignerOverride("S3SignerType");
            builder = builder.withClientConfiguration(config);
        }
        String endpoint = "http://" + host + ":" + port;
        String clientRegion = null;
        if (!ServiceUtils.isS3USStandardEndpoint(endpoint)) {
            clientRegion = AwsHostNameUtils.parseRegion(host, AmazonS3Client.S3_SERVICE_NAME);
        }
        builder = builder.withEndpointConfiguration(new EndpointConfiguration(endpoint, clientRegion));
        builder = builder.withPathStyleAccessEnabled(pathStyleAccess);
        s3Client = builder.build();
    }
    return s3Client;
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) ClientConfigurationFactory(com.amazonaws.ClientConfigurationFactory) EndpointConfiguration(com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) ClientConfiguration(com.amazonaws.ClientConfiguration)

Example 38 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project jaqy by Teradata.

the class S3Utils method getS3Client.

public static AmazonS3 getS3Client(JaqyInterpreter interpreter) {
    VariableManager vm = interpreter.getVariableManager();
    Variable clientVar = vm.getVariable(S3CLIENT_VAR);
    if (clientVar != null) {
        Object o = clientVar.get();
        if (o instanceof AmazonS3)
            return (AmazonS3) o;
    }
    // now we need to setup a new client.
    AmazonS3ClientBuilder builder = getS3Builder(interpreter);
    // check if we need to set up the access / secret key
    String access = null;
    String secret = null;
    {
        Variable var = vm.getVariable(S3ACCESS_VAR);
        if (var != null) {
            Object o = var.get();
            if (o != null)
                access = o.toString();
        }
    }
    {
        Variable var = vm.getVariable(S3SECRET_VAR);
        if (var != null) {
            Object o = var.get();
            if (o != null)
                secret = o.toString();
        }
    }
    if (access != null && secret != null) {
        /*
			 * When both access and secret are null, we are using the default
			 * values (i.e. from credential file or env variables etc).
			 * 
			 * When both are set, then we override the default settings (and
			 * subsequent uses).
			 */
        if (access.length() == 0 && secret.length() == 0) {
            /*
				 * This is for accessing publicly accessible buckets.
				 */
            builder.withCredentials(new AWSStaticCredentialsProvider(new AnonymousAWSCredentials()));
        } else {
            builder.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(access, secret)));
        }
    }
    AmazonS3 client = builder.build();
    // now save the client to s3client variable
    if (clientVar == null) {
        clientVar = new Variable() {

            private AmazonS3 m_client;

            @Override
            public Object get() {
                return m_client;
            }

            @Override
            public boolean set(Object value) {
                if (value != null && !(value instanceof AmazonS3))
                    return false;
                m_client = (AmazonS3) value;
                return true;
            }

            @Override
            public String getName() {
                return S3CLIENT_VAR;
            }
        };
    }
    clientVar.set(client);
    vm.setVariable(clientVar);
    return client;
}
Also used : VariableManager(com.teradata.jaqy.VariableManager) AmazonS3(com.amazonaws.services.s3.AmazonS3) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) Variable(com.teradata.jaqy.interfaces.Variable) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) AnonymousAWSCredentials(com.amazonaws.auth.AnonymousAWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Example 39 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project thingsboard by thingsboard.

the class SnsPlugin method init.

private void init() {
    AWSCredentials awsCredentials = new BasicAWSCredentials(configuration.getAccessKeyId(), configuration.getSecretAccessKey());
    AWSStaticCredentialsProvider credProvider = new AWSStaticCredentialsProvider(awsCredentials);
    AmazonSNS sns = AmazonSNSClient.builder().withCredentials(credProvider).withRegion(configuration.getRegion()).build();
    this.snsMessageHandler = new SnsMessageHandler(sns);
}
Also used : AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AWSCredentials(com.amazonaws.auth.AWSCredentials) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials) AmazonSNS(com.amazonaws.services.sns.AmazonSNS)

Example 40 with AWSStaticCredentialsProvider

use of com.amazonaws.auth.AWSStaticCredentialsProvider in project opencast by opencast.

the class AwsS3DistributionServiceImpl method activate.

@Override
public void activate(ComponentContext cc) {
    // Get the configuration
    if (cc != null) {
        if (!Boolean.valueOf(getAWSConfigKey(cc, AWS_S3_DISTRIBUTION_ENABLE))) {
            logger.info("AWS S3 distribution disabled");
            return;
        }
        // AWS S3 bucket name
        bucketName = getAWSConfigKey(cc, AWS_S3_BUCKET_CONFIG);
        logger.info("AWS S3 bucket name is {}", bucketName);
        // AWS region
        String regionStr = getAWSConfigKey(cc, AWS_S3_REGION_CONFIG);
        logger.info("AWS region is {}", regionStr);
        opencastDistributionUrl = getAWSConfigKey(cc, AWS_S3_DISTRIBUTION_BASE_CONFIG);
        if (!opencastDistributionUrl.endsWith("/")) {
            opencastDistributionUrl = opencastDistributionUrl + "/";
        }
        logger.info("AWS distribution url is {}", opencastDistributionUrl);
        // Explicit credentials are optional.
        AWSCredentialsProvider provider = null;
        Option<String> accessKeyIdOpt = OsgiUtil.getOptCfg(cc.getProperties(), AWS_S3_ACCESS_KEY_ID_CONFIG);
        Option<String> accessKeySecretOpt = OsgiUtil.getOptCfg(cc.getProperties(), AWS_S3_SECRET_ACCESS_KEY_CONFIG);
        // profile credentials
        if (accessKeyIdOpt.isNone() && accessKeySecretOpt.isNone())
            provider = new DefaultAWSCredentialsProviderChain();
        else
            provider = new AWSStaticCredentialsProvider(new BasicAWSCredentials(accessKeyIdOpt.get(), accessKeySecretOpt.get()));
        // Create AWS client.
        s3 = AmazonS3ClientBuilder.standard().withRegion(regionStr).withCredentials(provider).build();
        s3TransferManager = new TransferManager(s3);
        // Create AWS S3 bucket if not there yet
        createAWSBucket();
        distributionChannel = OsgiUtil.getComponentContextProperty(cc, CONFIG_KEY_STORE_TYPE);
        logger.info("AwsS3DistributionService activated!");
    }
}
Also used : DefaultAWSCredentialsProviderChain(com.amazonaws.auth.DefaultAWSCredentialsProviderChain) TransferManager(com.amazonaws.services.s3.transfer.TransferManager) AWSStaticCredentialsProvider(com.amazonaws.auth.AWSStaticCredentialsProvider) AWSCredentialsProvider(com.amazonaws.auth.AWSCredentialsProvider) BasicAWSCredentials(com.amazonaws.auth.BasicAWSCredentials)

Aggregations

AWSStaticCredentialsProvider (com.amazonaws.auth.AWSStaticCredentialsProvider)63 BasicAWSCredentials (com.amazonaws.auth.BasicAWSCredentials)44 AWSCredentials (com.amazonaws.auth.AWSCredentials)15 Test (org.junit.Test)15 AWSCredentialsProvider (com.amazonaws.auth.AWSCredentialsProvider)14 ClientConfiguration (com.amazonaws.ClientConfiguration)13 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)11 AmazonS3 (com.amazonaws.services.s3.AmazonS3)9 AmazonClientException (com.amazonaws.AmazonClientException)8 Regions (com.amazonaws.regions.Regions)8 SdkClientException (com.amazonaws.SdkClientException)7 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)7 BasicSessionCredentials (com.amazonaws.auth.BasicSessionCredentials)6 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)6 File (java.io.File)6 AmazonServiceException (com.amazonaws.AmazonServiceException)5 EndpointConfiguration (com.amazonaws.client.builder.AwsClientBuilder.EndpointConfiguration)5 AmazonEC2 (com.amazonaws.services.ec2.AmazonEC2)3 ObjectListing (com.amazonaws.services.s3.model.ObjectListing)3 AWSSecurityTokenService (com.amazonaws.services.securitytoken.AWSSecurityTokenService)3