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");
}
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;
}
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;
}
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);
}
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!");
}
}
Aggregations