Example usage for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

List of usage examples for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client

Introduction

In this page you can find the example usage for com.amazonaws.services.s3 AmazonS3Client AmazonS3Client.

Prototype

@SdkInternalApi
AmazonS3Client(AmazonS3ClientParams s3ClientParams) 

Source Link

Document

Constructs a new client to invoke service methods on S3 using the specified parameters.

Usage

From source file:onl.area51.filesystem.s3.AbstractS3Action.java

License:Apache License

public AbstractS3Action(FileSystemIO delegate, Map<String, ?> env) {
    this.delegate = delegate;

    AWSCredentials credentials;//from   w w  w.ja  va 2 s. c  o  m
    try {
        credentials = new ProfileCredentialsProvider().getCredentials();
    } catch (Exception ex) {
        throw new AmazonClientException("Cannot load the credentials from the credential profiles file. "
                + "Please make sure that your credentials file is at the correct "
                + "location (~/.aws/credentials), and is in valid format.", ex);
    }

    s3 = new AmazonS3Client(credentials);
    Region region = Region.getRegion(Regions.EU_WEST_1);
    s3.setRegion(region);

    String n = FileSystemUtils.get(env, BUCKET_READ);
    if (n == null || n.trim().isEmpty()) {
        n = FileSystemUtils.get(env, BUCKET);
    }
    bucketName = Objects.requireNonNull(n, BUCKET + " or " + BUCKET_READ + " is not defined");
}

From source file:opendap.aws.demo.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 */// ww  w  .  ja  v a 2s  . co m
private static void init() throws Exception {
    /*
    * This credentials provider implementation loads your AWS credentials
    * from a properties file at the root of your classpath.
    */
    //AWSCredentialsProvider credentialsProvider = new ClasspathPropertiesFileCredentialsProvider();

    AWSCredentialsProvider credentialsProvider = new CredentialsProvider();

    ec2 = new AmazonEC2Client(credentialsProvider);
    s3 = new AmazonS3Client(credentialsProvider);
    sdb = new AmazonSimpleDBClient(credentialsProvider);
}

From source file:opendap.aws.s3.SimpleS3Uploader.java

License:Open Source License

/**
 * The only information needed to create a client are security credentials
 * consisting of the AWS Access Key ID and Secret Access Key. All other
 * configuration, such as the service endpoints, are performed
 * automatically. Client parameters, such as proxies, can be specified in an
 * optional ClientConfiguration object when constructing a client.
 *
 * @see com.amazonaws.auth.BasicAWSCredentials
 * @see com.amazonaws.auth.PropertiesCredentials
 * @see com.amazonaws.ClientConfiguration
 *//*from www  .ja v a 2s  . co m*/
private void initS3() throws Exception {

    BasicAWSCredentials basicAWSCredentials = new BasicAWSCredentials(awsAccessKeyId, awsSecretKey);

    s3 = new AmazonS3Client(basicAWSCredentials);
}

From source file:org.akvo.flow.deploy.Deploy.java

License:Open Source License

private static void uploadS3(String accessKey, String secretKey, String s3Path, File file)
        throws AmazonServiceException, AmazonClientException {
    BasicAWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
    AmazonS3 s3 = new AmazonS3Client(credentials);

    PutObjectRequest putRequest = new PutObjectRequest(BUCKET_NAME, s3Path, file);
    ObjectMetadata metadata = new ObjectMetadata();

    // set content type as android package file
    metadata.setContentType("application/vnd.android.package-archive");

    // set content length to length of file
    metadata.setContentLength(file.length());

    // set access to public
    putRequest.setMetadata(metadata);/*w  w  w  .j a va2s  .co  m*/
    putRequest.setCannedAcl(CannedAccessControlList.PublicRead);

    // try to put the apk in S3
    PutObjectResult result = s3.putObject(putRequest);
    System.out.println("Apk uploaded successfully, with result ETag " + result.getETag());
}

From source file:org.akvo.flow.InstanceConfigurator.java

License:Open Source License

public static void main(String[] args) throws Exception {

    Options opts = getOptions();/*w w  w .  j av a2 s. co  m*/
    CommandLineParser parser = new BasicParser();
    CommandLine cli = null;

    try {
        cli = parser.parse(opts, args);
    } catch (Exception e) {
        System.err.println(e.getMessage());
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp(InstanceConfigurator.class.getName(), opts);
        System.exit(1);
    }

    String awsAccessKey = cli.getOptionValue("ak");
    String awsSecret = cli.getOptionValue("as");
    String bucketName = cli.getOptionValue("bn");
    String gaeId = cli.getOptionValue("gae");
    String outFolder = cli.getOptionValue("o");
    String flowServices = cli.getOptionValue("fs");
    String alias = cli.getOptionValue("a");
    String emailFrom = cli.getOptionValue("ef");
    String emailTo = cli.getOptionValue("et");
    String orgName = cli.getOptionValue("on");
    String signingKey = cli.getOptionValue("sk");

    File out = new File(outFolder);

    if (!out.exists()) {
        out.mkdirs();
    }

    Map<String, AccessKey> accessKeys = new HashMap<String, AccessKey>();
    String apiKey = UUID.randomUUID().toString().replaceAll("-", "");

    AWSCredentials creds = new BasicAWSCredentials(awsAccessKey, awsSecret);
    AmazonIdentityManagementClient iamClient = new AmazonIdentityManagementClient(creds);
    AmazonS3Client s3Client = new AmazonS3Client(creds);

    // Creating bucket

    System.out.println("Creating bucket: " + bucketName);

    try {
        if (s3Client.doesBucketExist(bucketName)) {
            System.out.println(bucketName + " already exists, skipping creation");
        } else {
            s3Client.createBucket(bucketName, Region.EU_Ireland);
        }
    } catch (Exception e) {
        System.err.println("Error trying to create bucket " + bucketName + " : " + e.getMessage());
        System.exit(1);
    }

    // Creating users and groups

    String gaeUser = bucketName + GAE_SUFFIX;
    String apkUser = bucketName + APK_SUFFIX;

    // GAE

    System.out.println("Creating user: " + gaeUser);

    GetUserRequest gaeUserRequest = new GetUserRequest();
    gaeUserRequest.setUserName(gaeUser);

    try {
        iamClient.getUser(gaeUserRequest);
        System.out.println("User already exists, skipping creation");
    } catch (NoSuchEntityException e) {
        iamClient.createUser(new CreateUserRequest(gaeUser));
    }

    System.out.println("Requesting security credentials for " + gaeUser);

    CreateAccessKeyRequest gaeAccessRequest = new CreateAccessKeyRequest();
    gaeAccessRequest.setUserName(gaeUser);

    CreateAccessKeyResult gaeAccessResult = iamClient.createAccessKey(gaeAccessRequest);
    accessKeys.put(gaeUser, gaeAccessResult.getAccessKey());

    // APK

    System.out.println("Creating user: " + apkUser);

    GetUserRequest apkUserRequest = new GetUserRequest();
    apkUserRequest.setUserName(apkUser);

    try {
        iamClient.getUser(apkUserRequest);
        System.out.println("User already exists, skipping creation");
    } catch (NoSuchEntityException e) {
        iamClient.createUser(new CreateUserRequest(apkUser));
    }

    System.out.println("Requesting security credentials for " + apkUser);

    CreateAccessKeyRequest apkAccessRequest = new CreateAccessKeyRequest();
    apkAccessRequest.setUserName(apkUser);

    CreateAccessKeyResult apkAccessResult = iamClient.createAccessKey(apkAccessRequest);
    accessKeys.put(apkUser, apkAccessResult.getAccessKey());

    System.out.println("Configuring security policies...");

    Configuration cfg = new Configuration();
    cfg.setClassForTemplateLoading(InstanceConfigurator.class, "/org/akvo/flow/templates");
    cfg.setObjectWrapper(new DefaultObjectWrapper());
    cfg.setDefaultEncoding("UTF-8");

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("bucketName", bucketName);
    data.put("version", new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
    data.put("accessKey", accessKeys);

    Template t1 = cfg.getTemplate("apk-s3-policy.ftl");
    StringWriter apkPolicy = new StringWriter();
    t1.process(data, apkPolicy);

    Template t2 = cfg.getTemplate("gae-s3-policy.ftl");
    StringWriter gaePolicy = new StringWriter();
    t2.process(data, gaePolicy);

    iamClient.putUserPolicy(
            new PutUserPolicyRequest(apkUser, apkUser, Policy.fromJson(apkPolicy.toString()).toJson()));

    iamClient.putUserPolicy(
            new PutUserPolicyRequest(gaeUser, gaeUser, Policy.fromJson(gaePolicy.toString()).toJson()));

    System.out.println("Creating configuration files...");

    // survey.properties
    Map<String, Object> apkData = new HashMap<String, Object>();
    apkData.put("awsBucket", bucketName);
    apkData.put("awsAccessKeyId", accessKeys.get(apkUser).getAccessKeyId());
    apkData.put("awsSecretKey", accessKeys.get(apkUser).getSecretAccessKey());
    apkData.put("serverBase", "https://" + gaeId + ".appspot.com");
    apkData.put("restApiKey", apiKey);

    Template t3 = cfg.getTemplate("survey.properties.ftl");
    FileWriter fw = new FileWriter(new File(out, "/survey.properties"));
    t3.process(apkData, fw);

    // appengine-web.xml
    Map<String, Object> webData = new HashMap<String, Object>();
    webData.put("awsBucket", bucketName);
    webData.put("awsAccessKeyId", accessKeys.get(gaeUser).getAccessKeyId());
    webData.put("awsSecretAccessKey", accessKeys.get(gaeUser).getSecretAccessKey());
    webData.put("s3url", "https://" + bucketName + ".s3.amazonaws.com");
    webData.put("instanceId", gaeId);
    webData.put("alias", alias);
    webData.put("flowServices", flowServices);
    webData.put("apiKey", apiKey);
    webData.put("emailFrom", emailFrom);
    webData.put("emailTo", emailTo);
    webData.put("organization", orgName);
    webData.put("signingKey", signingKey);

    Template t5 = cfg.getTemplate("appengine-web.xml.ftl");
    FileWriter fw3 = new FileWriter(new File(out, "/appengine-web.xml"));
    t5.process(webData, fw3);

    System.out.println("Done");
}

From source file:org.alanwilliamson.amazon.AmazonBase.java

License:Open Source License

/**
 * Returns back the necessary AmazonS3 class for communicating to the S3
 * //from www  .  j  a  va  2s.co m
 * @param _session
 * @param argStruct
 * @return
 * @throws cfmRunTimeException
 */
public AmazonS3 getAmazonS3(AmazonKey amazonKey) throws cfmRunTimeException {
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(amazonKey.getKey(), amazonKey.getSecret());
    AmazonS3 s3Client = new AmazonS3Client(awsCreds);
    s3Client.setRegion(amazonKey.getAmazonRegion().toAWSRegion());
    return s3Client;
}

From source file:org.anhonesteffort.p25.wav.TransferManagerFactory.java

License:Open Source License

private AmazonS3 amazonS3(AWSCredentialsProvider credentials) {
    return new AmazonS3Client(credentials);
}

From source file:org.apache.apex.malhar.lib.fs.s3.S3BlockUploadOperator.java

License:Apache License

/**
 * Create AmazonS3 client using AWS credentials
 * @return AmazonS3//ww  w  . j a  v a2  s. c om
 */
protected AmazonS3 createClient() {
    AmazonS3 client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretAccessKey));
    if (endPoint != null) {
        client.setEndpoint(endPoint);
    }
    return client;
}

From source file:org.apache.apex.malhar.lib.fs.s3.S3Reconciler.java

License:Apache License

@Override
public void setup(Context.OperatorContext context) {
    s3client = new AmazonS3Client(new BasicAWSCredentials(accessKey, secretKey));
    if (region != null) {
        s3client.setRegion(Region.getRegion(Regions.fromName(region)));
    }/*from w w  w.  j av a2 s .c  o  m*/
    filePath = context.getValue(DAG.APPLICATION_PATH);
    try {
        fs = FileSystem.newInstance(new Path(filePath).toUri(), new Configuration());
    } catch (IOException e) {
        logger.error("Unable to create FileSystem: {}", e.getMessage());
    }
    super.setup(context);
}

From source file:org.apache.camel.component.aws.s3.S3Endpoint.java

License:Apache License

/**
 * Provide the possibility to override this method for an mock implementation
 *
 * @return AmazonS3Client/*from  ww  w  .j av  a2 s .co m*/
 */
AmazonS3 createS3Client() {
    AWSCredentials credentials = new BasicAWSCredentials(configuration.getAccessKey(),
            configuration.getSecretKey());
    AmazonS3 client = new AmazonS3Client(credentials);
    if (configuration.getAmazonS3Endpoint() != null) {
        client.setEndpoint(configuration.getAmazonS3Endpoint());
    }
    return client;
}