home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Java example source code file (BaseS3.java)

This example Java source code file (BaseS3.java) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Learn more about this Java project at its project page.

Java - Java tags/keywords

amazonec2client, amazons3, amazons3client, aws_access_key, aws_access_key_id, aws_secret_access_key, aws_secret_key, bases3, basicawscredentials, exception, illegalstateexception, propertiescredentials, string, unable

The BaseS3.java Java example source code

/*
 *
 *  * Copyright 2015 Skymind,Inc.
 *  *
 *  *    Licensed under the Apache License, Version 2.0 (the "License");
 *  *    you may not use this file except in compliance with the License.
 *  *    You may obtain a copy of the License at
 *  *
 *  *        http://www.apache.org/licenses/LICENSE-2.0
 *  *
 *  *    Unless required by applicable law or agreed to in writing, software
 *  *    distributed under the License is distributed on an "AS IS" BASIS,
 *  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  *    See the License for the specific language governing permissions and
 *  *    limitations under the License.
 *
 */

package org.deeplearning4j.aws.s3;

import java.io.File;
import java.io.InputStream;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.services.ec2.AmazonEC2;
import com.amazonaws.services.ec2.AmazonEC2Client;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;


/**
 * The S3 Credentials works via discovering the credentials
 * from the system properties (passed in via -D or System wide)
 * If you invoke the JVM with -Dorg.deeplearning4j.aws.accessKey=YOUR_ACCESS_KEY
 * and -Dorg.deeplearning4j.aws.accessSecret=YOUR_SECRET_KEY
 * this will pick up the credentials from there, otherwise it will also attempt to look in
 * the system environment for the following variables:
 * 
 * 
 * AWS_ACCESS_KEY_ID
 * AWS_SECRET_ACCESS_KEY
 * @author Adam Gibson
 *
 */
public abstract class BaseS3 {


	/**
	 * 
	 */
	protected static final long serialVersionUID = -2280107690193651289L;
	protected String accessKey;
	protected String secretKey;
	protected AWSCredentials creds;
	public final static String ACCESS_KEY = "org.deeplearning4j.aws.accessKey";
	public final static String  ACCESS_SECRET = "org.deeplearning4j.aws.accessSecret";
	public final static String AWS_ACCESS_KEY = "AWS_ACCESS_KEY"; //"AWS_ACCESS_KEY_ID";
	public final static String AWS_SECRET_KEY = "AWS_SECRET_KEY"; //"AWS_SECRET_ACCESS_KEY";
	
	
	protected void findCreds() {
		if(System.getProperty(ACCESS_KEY) != null && System.getProperty(ACCESS_SECRET) != null) {
			accessKey = System.getProperty(ACCESS_KEY);
			secretKey = System.getProperty(ACCESS_SECRET);
		}
		
		else if(System.getenv(AWS_ACCESS_KEY) != null && System.getenv(AWS_SECRET_KEY) != null) {
			accessKey = System.getenv(AWS_ACCESS_KEY);
			secretKey = System.getenv(AWS_SECRET_KEY);
		}
	}
	
	public BaseS3() {
		findCreds();
		if(accessKey != null && secretKey != null)
			creds = new BasicAWSCredentials(accessKey,secretKey);
		if(creds == null)
			throw new IllegalStateException("Unable to find ec2 credentials");
	}
	
	public BaseS3(File file) throws Exception {
		if(accessKey != null && secretKey != null)
			creds = new BasicAWSCredentials(accessKey,secretKey);
		else 
			creds = new PropertiesCredentials(file);
			
		
	}
	
	public BaseS3(InputStream is) throws Exception {
		if(accessKey != null && secretKey != null)
			creds = new BasicAWSCredentials(accessKey,secretKey);
		else 
			creds = new PropertiesCredentials(is);
			
		
	}

	public AWSCredentials getCreds() {
		return creds;
	}

	public void setCreds(AWSCredentials creds) {
		this.creds = creds;
	}
	
	public AmazonS3 getClient() {
		return new AmazonS3Client(creds);
	}
	
	public AmazonEC2 getEc2() {
		
		return new AmazonEC2Client(creds);
	}
	

}

Other Java examples (source code examples)

Here is a short list of links related to this Java BaseS3.java source code file:



my book on functional programming

 

new blog posts

 

Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.