List of usage examples for com.amazonaws ClientConfiguration setProtocol
public void setProtocol(Protocol protocol)
From source file:maebackup.MaeBackup.java
License:Open Source License
public static void list(String arg) { try {// ww w.j a v a2 s. c o m System.out.println("Listing Glacier vault..."); ClientConfiguration config = new ClientConfiguration(); config.setProtocol(Protocol.HTTPS); AmazonGlacierClient client = new AmazonGlacierClient(credentials, config); client.setEndpoint(endpoint); if (arg == null || arg == "") { InitiateJobResult result = client.initiateJob( new InitiateJobRequest(vaultname, new JobParameters().withType("inventory-retrieval"))); String jobid = result.getJobId(); System.out.println("Started inventory retrival job as ID " + jobid); } else { DescribeJobResult djres = client.describeJob(new DescribeJobRequest(vaultname, arg)); if (!djres.getStatusCode().equals("Succeeded")) { System.out.println("Job is not listed as Succeeded. It is: " + djres.getStatusCode()); System.out.println(djres.getStatusMessage()); System.exit(2); } GetJobOutputResult gjores = client .getJobOutput(new GetJobOutputRequest().withVaultName(vaultname).withJobId(arg)); byte[] buffer = new byte[1024]; int bytes; while ((bytes = gjores.getBody().read(buffer)) > 0) { System.out.write(buffer, 0, bytes); } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:n3phele.factory.ec2.VirtualServerResource.java
License:Open Source License
@GET @RolesAllowed("authenticated") @Path("dump") public String dump(@QueryParam("id") String id, @QueryParam("key") String key, @DefaultValue("https://ec2.amazonaws.com") @QueryParam("location") String location) { log.info("Id=" + id + " key=" + key); ClientConfiguration clientConfiguration = new ClientConfiguration(); try {//from w ww . j av a 2 s . c om clientConfiguration .setProtocol(Protocol.valueOf(URI.create(location).toURL().getProtocol().toUpperCase())); } catch (MalformedURLException e) { throw new WebApplicationException(); } AmazonEC2Client client = new AmazonEC2Client(new BasicAWSCredentials(id, key), clientConfiguration); client.setEndpoint(location.toString()); DescribeKeyPairsResult result = client.describeKeyPairs(); log.info("Key pairs " + result.getKeyPairs()); return (result.getKeyPairs().size() + " key pairs "); }
From source file:n3phele.factory.ec2.VirtualServerResource.java
License:Open Source License
private AmazonEC2Client getEC2Client(String accessKey, String encryptedKey, URI location) { AWSCredentials credentials = null;//from w ww . j av a 2s .c o m try { credentials = new EncryptedAWSCredentials(accessKey, encryptedKey); } catch (UnsupportedEncodingException e) { throw new WebApplicationException(); } catch (NoSuchAlgorithmException e) { throw new WebApplicationException(); } catch (Exception e) { throw new WebApplicationException(); } ClientConfiguration clientConfiguration = new ClientConfiguration(); try { clientConfiguration.setProtocol(Protocol.valueOf(location.toURL().getProtocol().toUpperCase())); } catch (MalformedURLException e) { throw new WebApplicationException(); } AmazonEC2Client client = new AmazonEC2Client(credentials, clientConfiguration); client.setEndpoint(location.toString()); return client; }
From source file:org.apache.hadoop.fs.s3a.S3AFileSystem.java
License:Apache License
/** Called after a new FileSystem instance is constructed. * @param name a uri whose authority section names the host, port, etc. * for this FileSystem/* www.j av a 2 s .c o m*/ * @param conf the configuration */ public void initialize(URI name, Configuration conf) throws IOException { super.initialize(name, conf); uri = URI.create(name.getScheme() + "://" + name.getAuthority()); workingDir = new Path("/user", System.getProperty("user.name")).makeQualified(this.uri, this.getWorkingDirectory()); // Try to get our credentials or just connect anonymously String accessKey = conf.get(NEW_ACCESS_KEY, conf.get(OLD_ACCESS_KEY, null)); String secretKey = conf.get(NEW_SECRET_KEY, conf.get(OLD_SECRET_KEY, null)); String userInfo = name.getUserInfo(); if (userInfo != null) { int index = userInfo.indexOf(':'); if (index != -1) { accessKey = userInfo.substring(0, index); secretKey = userInfo.substring(index + 1); } else { accessKey = userInfo; } } AWSCredentialsProviderChain credentials = new AWSCredentialsProviderChain( new BasicAWSCredentialsProvider(accessKey, secretKey), new InstanceProfileCredentialsProvider(), new AnonymousAWSCredentialsProvider()); bucket = name.getHost(); ClientConfiguration awsConf = new ClientConfiguration(); awsConf.setMaxConnections(conf.getInt(NEW_MAXIMUM_CONNECTIONS, conf.getInt(OLD_MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS))); awsConf.setProtocol(conf.getBoolean(NEW_SECURE_CONNECTIONS, conf.getBoolean(OLD_SECURE_CONNECTIONS, DEFAULT_SECURE_CONNECTIONS)) ? Protocol.HTTPS : Protocol.HTTP); awsConf.setMaxErrorRetry( conf.getInt(NEW_MAX_ERROR_RETRIES, conf.getInt(OLD_MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES))); awsConf.setSocketTimeout( conf.getInt(NEW_SOCKET_TIMEOUT, conf.getInt(OLD_SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT))); s3 = new AmazonS3Client(credentials, awsConf); maxKeys = conf.getInt(NEW_MAX_PAGING_KEYS, conf.getInt(OLD_MAX_PAGING_KEYS, DEFAULT_MAX_PAGING_KEYS)); partSize = conf.getLong(NEW_MULTIPART_SIZE, conf.getLong(OLD_MULTIPART_SIZE, DEFAULT_MULTIPART_SIZE)); partSizeThreshold = conf.getLong(NEW_MIN_MULTIPART_THRESHOLD, conf.getLong(OLD_MIN_MULTIPART_THRESHOLD, DEFAULT_MIN_MULTIPART_THRESHOLD)); if (partSize < 5 * 1024 * 1024) { LOG.error(NEW_MULTIPART_SIZE + " must be at least 5 MB"); partSize = 5 * 1024 * 1024; } if (partSizeThreshold < 5 * 1024 * 1024) { LOG.error(NEW_MIN_MULTIPART_THRESHOLD + " must be at least 5 MB"); partSizeThreshold = 5 * 1024 * 1024; } String cannedACLName = conf.get(NEW_CANNED_ACL, conf.get(OLD_CANNED_ACL, DEFAULT_CANNED_ACL)); if (!cannedACLName.isEmpty()) { cannedACL = CannedAccessControlList.valueOf(cannedACLName); } else { cannedACL = null; } if (!s3.doesBucketExist(bucket)) { throw new IOException("Bucket " + bucket + " does not exist"); } boolean purgeExistingMultipart = conf.getBoolean(NEW_PURGE_EXISTING_MULTIPART, conf.getBoolean(OLD_PURGE_EXISTING_MULTIPART, DEFAULT_PURGE_EXISTING_MULTIPART)); long purgeExistingMultipartAge = conf.getLong(NEW_PURGE_EXISTING_MULTIPART_AGE, conf.getLong(OLD_PURGE_EXISTING_MULTIPART_AGE, DEFAULT_PURGE_EXISTING_MULTIPART_AGE)); if (purgeExistingMultipart) { TransferManager transferManager = new TransferManager(s3); Date purgeBefore = new Date(new Date().getTime() - purgeExistingMultipartAge * 1000); transferManager.abortMultipartUploads(bucket, purgeBefore); transferManager.shutdownNow(false); } serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM, null); setConf(conf); }
From source file:org.apache.hadoop.fs.s3r.S3RFileSystem.java
License:Apache License
/** Called after a new FileSystem instance is constructed. * @param name a uri whose authority section names the host, port, etc. * for this FileSystem/* w w w . j av a2 s . c o m*/ * @param conf the configuration */ public void initialize(URI name, Configuration conf) throws IOException { super.initialize(name, conf); uri = URI.create(name.getScheme() + "://" + name.getAuthority()); workingDir = new Path("/user", System.getProperty("user.name")).makeQualified(this.uri, this.getWorkingDirectory()); // Try to get our credentials or just connect anonymously String accessKey = conf.get(ACCESS_KEY, null); String secretKey = conf.get(SECRET_KEY, null); String userInfo = name.getUserInfo(); if (userInfo != null) { int index = userInfo.indexOf(':'); if (index != -1) { accessKey = userInfo.substring(0, index); secretKey = userInfo.substring(index + 1); } else { accessKey = userInfo; } } AWSCredentialsProviderChain credentials = new AWSCredentialsProviderChain( new BasicAWSCredentialsProvider(accessKey, secretKey), new InstanceProfileCredentialsProvider(), new AnonymousAWSCredentialsProvider()); bucket = name.getHost(); ClientConfiguration awsConf = new ClientConfiguration(); awsConf.setMaxConnections(conf.getInt(MAXIMUM_CONNECTIONS, DEFAULT_MAXIMUM_CONNECTIONS)); boolean secureConnections = conf.getBoolean(SECURE_CONNECTIONS, DEFAULT_SECURE_CONNECTIONS); awsConf.setProtocol(secureConnections ? Protocol.HTTPS : Protocol.HTTP); awsConf.setMaxErrorRetry(conf.getInt(MAX_ERROR_RETRIES, DEFAULT_MAX_ERROR_RETRIES)); awsConf.setConnectionTimeout(conf.getInt(ESTABLISH_TIMEOUT, DEFAULT_ESTABLISH_TIMEOUT)); awsConf.setSocketTimeout(conf.getInt(SOCKET_TIMEOUT, DEFAULT_SOCKET_TIMEOUT)); String proxyHost = conf.getTrimmed(PROXY_HOST, ""); int proxyPort = conf.getInt(PROXY_PORT, -1); if (!proxyHost.isEmpty()) { awsConf.setProxyHost(proxyHost); if (proxyPort >= 0) { awsConf.setProxyPort(proxyPort); } else { if (secureConnections) { LOG.warn("Proxy host set without port. Using HTTPS default 443"); awsConf.setProxyPort(443); } else { LOG.warn("Proxy host set without port. Using HTTP default 80"); awsConf.setProxyPort(80); } } String proxyUsername = conf.getTrimmed(PROXY_USERNAME); String proxyPassword = conf.getTrimmed(PROXY_PASSWORD); if ((proxyUsername == null) != (proxyPassword == null)) { String msg = "Proxy error: " + PROXY_USERNAME + " or " + PROXY_PASSWORD + " set without the other."; LOG.error(msg); throw new IllegalArgumentException(msg); } awsConf.setProxyUsername(proxyUsername); awsConf.setProxyPassword(proxyPassword); awsConf.setProxyDomain(conf.getTrimmed(PROXY_DOMAIN)); awsConf.setProxyWorkstation(conf.getTrimmed(PROXY_WORKSTATION)); if (LOG.isDebugEnabled()) { LOG.debug( "Using proxy server {}:{} as user {} with password {} on " + "domain {} as workstation {}", awsConf.getProxyHost(), awsConf.getProxyPort(), String.valueOf(awsConf.getProxyUsername()), awsConf.getProxyPassword(), awsConf.getProxyDomain(), awsConf.getProxyWorkstation()); } } else if (proxyPort >= 0) { String msg = "Proxy error: " + PROXY_PORT + " set without " + PROXY_HOST; LOG.error(msg); throw new IllegalArgumentException(msg); } s3 = new AmazonS3Client(credentials, awsConf); String endPoint = conf.getTrimmed(ENDPOINT, ""); if (!endPoint.isEmpty()) { try { s3.setEndpoint(endPoint); } catch (IllegalArgumentException e) { String msg = "Incorrect endpoint: " + e.getMessage(); LOG.error(msg); throw new IllegalArgumentException(msg, e); } } maxKeys = conf.getInt(MAX_PAGING_KEYS, DEFAULT_MAX_PAGING_KEYS); partSize = conf.getLong(MULTIPART_SIZE, DEFAULT_MULTIPART_SIZE); multiPartThreshold = conf.getInt(MIN_MULTIPART_THRESHOLD, DEFAULT_MIN_MULTIPART_THRESHOLD); if (partSize < 5 * 1024 * 1024) { LOG.error(MULTIPART_SIZE + " must be at least 5 MB"); partSize = 5 * 1024 * 1024; } if (multiPartThreshold < 5 * 1024 * 1024) { LOG.error(MIN_MULTIPART_THRESHOLD + " must be at least 5 MB"); multiPartThreshold = 5 * 1024 * 1024; } int maxThreads = conf.getInt(MAX_THREADS, DEFAULT_MAX_THREADS); int coreThreads = conf.getInt(CORE_THREADS, DEFAULT_CORE_THREADS); if (maxThreads == 0) { maxThreads = Runtime.getRuntime().availableProcessors() * 8; } if (coreThreads == 0) { coreThreads = Runtime.getRuntime().availableProcessors() * 8; } long keepAliveTime = conf.getLong(KEEPALIVE_TIME, DEFAULT_KEEPALIVE_TIME); LinkedBlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>( maxThreads * conf.getInt(MAX_TOTAL_TASKS, DEFAULT_MAX_TOTAL_TASKS)); threadPoolExecutor = new ThreadPoolExecutor(coreThreads, maxThreads, keepAliveTime, TimeUnit.SECONDS, workQueue, newDaemonThreadFactory("s3a-transfer-shared-")); threadPoolExecutor.allowCoreThreadTimeOut(true); TransferManagerConfiguration transferConfiguration = new TransferManagerConfiguration(); transferConfiguration.setMinimumUploadPartSize(partSize); transferConfiguration.setMultipartUploadThreshold(multiPartThreshold); transfers = new TransferManager(s3, threadPoolExecutor); transfers.setConfiguration(transferConfiguration); String cannedACLName = conf.get(CANNED_ACL, DEFAULT_CANNED_ACL); if (!cannedACLName.isEmpty()) { cannedACL = CannedAccessControlList.valueOf(cannedACLName); } else { cannedACL = null; } if (!s3.doesBucketExist(bucket)) { throw new IOException("Bucket " + bucket + " does not exist"); } boolean purgeExistingMultipart = conf.getBoolean(PURGE_EXISTING_MULTIPART, DEFAULT_PURGE_EXISTING_MULTIPART); long purgeExistingMultipartAge = conf.getLong(PURGE_EXISTING_MULTIPART_AGE, DEFAULT_PURGE_EXISTING_MULTIPART_AGE); if (purgeExistingMultipart) { Date purgeBefore = new Date(new Date().getTime() - purgeExistingMultipartAge * 1000); transfers.abortMultipartUploads(bucket, purgeBefore); } serverSideEncryptionAlgorithm = conf.get(SERVER_SIDE_ENCRYPTION_ALGORITHM); setConf(conf); }
From source file:org.apache.jackrabbit.oak.blob.cloud.aws.s3.Utils.java
License:Apache License
private static ClientConfiguration getClientConfiguration(Properties prop) { int connectionTimeOut = Integer.parseInt(prop.getProperty(S3Constants.S3_CONN_TIMEOUT)); int socketTimeOut = Integer.parseInt(prop.getProperty(S3Constants.S3_SOCK_TIMEOUT)); int maxConnections = Integer.parseInt(prop.getProperty(S3Constants.S3_MAX_CONNS)); int maxErrorRetry = Integer.parseInt(prop.getProperty(S3Constants.S3_MAX_ERR_RETRY)); String protocol = prop.getProperty(S3Constants.S3_CONN_PROTOCOL); String proxyHost = prop.getProperty(S3Constants.PROXY_HOST); String proxyPort = prop.getProperty(S3Constants.PROXY_PORT); ClientConfiguration cc = new ClientConfiguration(); if (protocol != null && protocol.equalsIgnoreCase("http")) { cc.setProtocol(Protocol.HTTP); }//from www.jav a 2 s . c o m if (proxyHost != null && !proxyHost.isEmpty()) { cc.setProxyHost(proxyHost); } if (proxyPort != null && !proxyPort.isEmpty()) { cc.setProxyPort(Integer.parseInt(proxyPort)); } cc.setConnectionTimeout(connectionTimeOut); cc.setSocketTimeout(socketTimeOut); cc.setMaxConnections(maxConnections); cc.setMaxErrorRetry(maxErrorRetry); return cc; }
From source file:org.apache.nifi.processors.aws.AbstractAWSProcessor.java
License:Apache License
protected ClientConfiguration createConfiguration(final ProcessContext context) { final ClientConfiguration config = new ClientConfiguration(); config.setMaxConnections(context.getMaxConcurrentTasks()); config.setMaxErrorRetry(0);// w w w . jav a 2 s .c om config.setUserAgent(DEFAULT_USER_AGENT); // If this is changed to be a property, ensure other uses are also changed config.setProtocol(DEFAULT_PROTOCOL); final int commsTimeout = context.getProperty(TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue(); config.setConnectionTimeout(commsTimeout); config.setSocketTimeout(commsTimeout); final SSLContextService sslContextService = context.getProperty(SSL_CONTEXT_SERVICE) .asControllerService(SSLContextService.class); if (sslContextService != null) { final SSLContext sslContext = sslContextService.createSSLContext(SSLContextService.ClientAuth.NONE); // NIFI-3788: Changed hostnameVerifier from null to DHV (BrowserCompatibleHostnameVerifier is deprecated) SdkTLSSocketFactory sdkTLSSocketFactory = new SdkTLSSocketFactory(sslContext, new DefaultHostnameVerifier()); config.getApacheHttpClientConfig().setSslSocketFactory(sdkTLSSocketFactory); } if (context.getProperty(PROXY_HOST).isSet()) { String proxyHost = context.getProperty(PROXY_HOST).evaluateAttributeExpressions().getValue(); config.setProxyHost(proxyHost); Integer proxyPort = context.getProperty(PROXY_HOST_PORT).evaluateAttributeExpressions().asInteger(); config.setProxyPort(proxyPort); } return config; }
From source file:org.apache.streams.amazon.kinesis.KinesisPersistReader.java
License:Apache License
@Override public void prepare(Object configurationObject) { // Connect to Kinesis synchronized (this) { // Create the credentials Object AWSCredentials credentials = new BasicAWSCredentials(config.getKey(), config.getSecretKey()); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.valueOf(config.getProtocol().toString())); this.client = new AmazonKinesisClient(credentials, clientConfig); if (StringUtils.isNotEmpty(config.getRegion())) this.client.setRegion(Region.getRegion(Regions.fromName(config.getRegion()))); }/*from ww w . jav a2s . c om*/ streamNames = this.config.getStreams(); executor = Executors.newFixedThreadPool(streamNames.size()); }
From source file:org.apache.streams.amazon.kinesis.KinesisPersistWriter.java
License:Apache License
@Override public void prepare(Object configurationObject) { // Connect to Kinesis synchronized (this) { // Create the credentials Object AWSCredentials credentials = new BasicAWSCredentials(config.getKey(), config.getSecretKey()); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.valueOf(config.getProtocol().toString())); this.client = new AmazonKinesisClient(credentials, clientConfig); if (StringUtils.isNotEmpty(config.getRegion())) { this.client.setRegion(Region.getRegion(Regions.fromName(config.getRegion()))); }//from w w w. ja v a 2 s . c om } executor = Executors.newSingleThreadExecutor(); }
From source file:org.apache.streams.s3.S3PersistReader.java
License:Apache License
public void prepare(Object configurationObject) { // Connect to S3 synchronized (this) { // Create the credentials Object AWSCredentials credentials = new BasicAWSCredentials(s3ReaderConfiguration.getKey(), s3ReaderConfiguration.getSecretKey()); ClientConfiguration clientConfig = new ClientConfiguration(); clientConfig.setProtocol(Protocol.valueOf(s3ReaderConfiguration.getProtocol().toString())); // We do not want path style access S3ClientOptions clientOptions = new S3ClientOptions(); clientOptions.setPathStyleAccess(false); this.amazonS3Client = new AmazonS3Client(credentials, clientConfig); if (!Strings.isNullOrEmpty(s3ReaderConfiguration.getRegion())) this.amazonS3Client .setRegion(Region.getRegion(Regions.fromName(s3ReaderConfiguration.getRegion()))); this.amazonS3Client.setS3ClientOptions(clientOptions); }/* ww w. ja v a2 s. co m*/ final ListObjectsRequest request = new ListObjectsRequest() .withBucketName(this.s3ReaderConfiguration.getBucket()) .withPrefix(s3ReaderConfiguration.getReaderPath()).withMaxKeys(500); ObjectListing listing = this.amazonS3Client.listObjects(request); this.files = new ArrayList<String>(); /** * If you can list files that are in this path, then you must be dealing with a directory * if you cannot list files that are in this path, then you are most likely dealing with * a simple file. */ boolean hasCommonPrefixes = listing.getCommonPrefixes().size() > 0 ? true : false; boolean hasObjectSummaries = listing.getObjectSummaries().size() > 0 ? true : false; if (hasCommonPrefixes || hasObjectSummaries) { // Handle the 'directory' use case do { if (hasCommonPrefixes) { for (String file : listing.getCommonPrefixes()) { this.files.add(file); } } else { for (final S3ObjectSummary objectSummary : listing.getObjectSummaries()) { this.files.add(objectSummary.getKey()); } } // get the next batch. listing = this.amazonS3Client.listNextBatchOfObjects(listing); } while (listing.isTruncated()); } else { // handle the single file use-case this.files.add(s3ReaderConfiguration.getReaderPath()); } if (this.files.size() <= 0) LOGGER.error("There are no files to read"); this.persistQueue = Queues.synchronizedQueue(new LinkedBlockingQueue<StreamsDatum>(10000)); this.executor = Executors.newSingleThreadExecutor(); }