Created
March 31, 2015 14:43
-
-
Save colinmarc/de4fc8e2f2805a3e02e9 to your computer and use it in GitHub Desktop.
HadoopS3CredentialsProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.hadoop.conf.Configuration; | |
import com.amazonaws.AmazonClientException; | |
import com.amazonaws.auth.AWSCredentials; | |
import com.amazonaws.auth.AWSCredentialsProvider; | |
import com.amazonaws.auth.BasicAWSCredentials; | |
public class HadoopS3CredentialsProvider implements AWSCredentialsProvider { | |
private Configuration conf; | |
public HadoopS3CredentialsProvider(Configuration conf) { | |
this.conf = conf; | |
} | |
public AWSCredentials getCredentials() { | |
String accessKeyId = conf.get("fs.s3n.awsAccessKeyId"); | |
String secretAccessKey = conf.get("fs.s3n.awsSecretAccessKey"); | |
if (accessKeyId != null && secretAccessKey != null) | |
return new BasicAWSCredentials(accessKeyId, secretAccessKey); | |
throw new AmazonClientException("Unable to load AWS credentials from hadoop configuration"); | |
} | |
public void refresh() {} | |
@Override | |
public String toString() { | |
return getClass().getSimpleName(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// use like this | |
AmazonS3 s3 = new AmazonS3Client(new AWSCredentialsProviderChain( | |
new HadoopS3CredentialsProvider(conf), new DefaultAWSCredentialsProviderChain())); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment