Ejemplo n.º 1
0
 public static Region getRegion(String regionName) {
   if (StringUtils.isNotEmpty(regionName)) {
     return RegionUtils.getRegion(regionName);
   } else {
     return Region.getRegion(Regions.US_EAST_1);
   }
 }
Ejemplo n.º 2
0
  @Override
  public Profile findOne(String profileName) {
    AmazonDynamoDB client = null;
    if (System.getProperty("useProxy") != null) {
      ClientConfiguration conf =
          new ClientConfiguration()
              .withProxyHost(System.getProperty("proxyHost"))
              .withProxyPort(Integer.valueOf(System.getProperty("proxyPort")))
              .withProxyUsername(System.getProperty("proxyUsername"))
              .withProxyPassword(System.getProperty("proxyPassword"));
      client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider(), conf);
    } else {
      client = new AmazonDynamoDBClient(new ClasspathPropertiesFileCredentialsProvider());
    }

    client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1));
    client.setEndpoint("https://dynamodb.ap-northeast-1.amazonaws.com");
    DynamoDBMapper mapper = new DynamoDBMapper(client);
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
    Map<String, Condition> scanFilter = new HashMap<String, Condition>();
    Condition scanCondition =
        new Condition()
            .withComparisonOperator(ComparisonOperator.EQ)
            .withAttributeValueList(new AttributeValue().withS(profileName));
    scanFilter.put("profile_name", scanCondition);
    scanExpression.setScanFilter(scanFilter);
    for (Profile p : mapper.scan(Profile.class, scanExpression)) {
      return p;
    }
    return null;
  }
Ejemplo n.º 3
0
  public static void main(String[] args) {

    AmazonDynamoDBClient client = new AmazonDynamoDBClient(new ProfileCredentialsProvider());
    client.setRegion(Region.getRegion(Regions.US_WEST_2));

    DynamoDBMapper mapper = new DynamoDBMapper(client);

    // retrieve an object using a "normal" (non-composite) id
    Movie m = new Movie();
    m.setTitle("foxy brown");
    Movie m2 = mapper.load(m);
    if (m2.getExtra() != null) {
      System.out.println("title:" + m2.getTitle() + " extra:" + m2.getExtra());
    }

    // retrieve an object using a composite key
    Artist a = new Artist();
    a.setId(5);
    a.setName("Van Gogh");
    Artist a2 = mapper.load(a);

    // scan through a bunch of entries in a table and print them out
    ScanRequest scanRequest = new ScanRequest().withTableName("Artists");
    ScanResult result = client.scan(scanRequest);
    for (Map<String, AttributeValue> item : result.getItems()) {
      String o1 = item.get("id").getN();
      String o2 = item.get("name").getS();
      Artist artist = mapper.load(Artist.class, new Integer(o1), o2);
      artist.dump();
    }
  }
  private File retrieveS3File(String sqsdMessageBody) throws UnsupportedEncodingException {
    File localFile = null;

    if (!sqsdMessageBody.isEmpty()) {

      AmazonS3 s3 = new AmazonS3Client();

      List<S3EventNotificationRecord> records =
          S3EventNotification.parseJson(sqsdMessageBody).getRecords();

      S3EventNotificationRecord firstRecord = records.get(0);

      String bucketName = firstRecord.getS3().getBucket().getName();

      String objectRegion = firstRecord.getAwsRegion();
      Region s3Region = Region.getRegion(Regions.fromName(objectRegion));
      s3.setRegion(s3Region);

      // Object key may have spaces or unicode non-ASCII characters.
      String keyName = firstRecord.getS3().getObject().getKey().replace('+', ' ');
      keyName = URLDecoder.decode(keyName, "UTF-8");

      localFile = new File(keyName);

      System.out.println("Downloading file: " + objectRegion + "/" + bucketName + "/" + keyName);
      s3.getObject(new GetObjectRequest(bucketName, keyName), localFile);

      if (!localFile.canRead()) {
        localFile = null;
      }
    }
    return localFile;
  }
Ejemplo n.º 5
0
  public static void main(String[] args) throws UnknownHostException {
    AmazonEC2 ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest2);
    List<String> instanceIds = new ArrayList<>();
    String instanceid = null;
    DescribeInstancesRequest request = new DescribeInstancesRequest();

    List<String> valuesT1 = new ArrayList<String>();
    valuesT1.add("hz-nodes");
    Filter filter = new Filter("tag-value", valuesT1);

    DescribeInstancesResult result = ec2.describeInstances(request.withFilters(filter));

    List<Reservation> reservations = result.getReservations();

    for (Reservation reservation : reservations) {
      List<Instance> instances = reservation.getInstances();
      for (Instance instance : instances) {
        instanceid = instance.getInstanceId();
        PrintManager.PrintMessage(instanceid);
        instanceIds.add(instanceid);
        List<Tag> abc = instance.getTags();
        for (Tag aaa : abc) {
          PrintManager.PrintMessage(aaa.getKey() + " : " + aaa.getValue());
        }
      }
    }
    instanceIds.remove("i-cb45adfc");
    StopInstancesRequest stopReq = new StopInstancesRequest(instanceIds);
    ec2.stopInstances(stopReq);
    RebootInstancesRequest requestRe = new RebootInstancesRequest(instanceIds);
    ec2.rebootInstances(requestRe);
  }
  @Bean
  public AmazonSimpleWorkflow amazonSimpleWorkflow() {
    AmazonSimpleWorkflow workflow =
        new AmazonSimpleWorkflowClient(awsCredentials(), clientConfiguration());
    workflow.setRegion(Region.getRegion(Regions.fromName(amazonSWFProperties.getRegionId())));

    return workflow;
  }
Ejemplo n.º 7
0
 /**
  * Gets an instance of a S3 client which is constructed using the given Context.
  *
  * @param context An Context instance.
  * @return A default S3 client.
  */
 public static AmazonS3Client getS3Client(Context context) {
   if (sS3Client == null) {
     sS3Client = new AmazonS3Client(getCredProvider(context.getApplicationContext()));
     // Set the region of your S3 bucket
     sS3Client.setRegion(Region.getRegion(Regions.US_EAST_1));
   }
   return sS3Client;
 }
Ejemplo n.º 8
0
 public AuthDaoImpl(AwsRegionCreds credentials) {
   try {
     AmazonDynamoDBClient client = new AmazonDynamoDBClient(credentials);
     client.setRegion(Region.getRegion(credentials.getAWSRegion()));
     db = new DynamoDBMapper(client);
   } catch (Exception e) {
     throw new RuntimeException(e);
   }
 }
Ejemplo n.º 9
0
  private AmazonS3 getAmzS3Client() {

    BasicAWSCredentials awsCreds = new BasicAWSCredentials(accessKeyId, secretAccessKey);
    AmazonS3 s3Client = new AmazonS3Client(awsCreds);
    Region region = Region.getRegion(Regions.EU_WEST_1);
    s3Client.setRegion(region);

    return s3Client;
  }
Ejemplo n.º 10
0
  public static void main(String args[]) {
    AmazonEC2 amazonEC2Client =
        new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_WEST_2); // Getting Region
    amazonEC2Client.setRegion(usWest2); // Setting US West (Oregon) region

    DeleteKeyPairRequest deleteKeyPairRequest = new DeleteKeyPairRequest("TestKey");
    amazonEC2Client.deleteKeyPair(deleteKeyPairRequest);
  }
Ejemplo n.º 11
0
  public static void setUpEc2ClientOject() {

    // Create the AmazonEC2Client object.
    ec2 = new AmazonEC2Client(awsCredentials());
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    ec2.setRegion(usEast1);

    addSecurityGroup("SecurityGroupForLoadGenerator");
    addSecurityGroup("SecurityGroupForELBAndAutoscale");
  }
 public HighScoreList(int sortMethod) {
   // Initial the SimpleDB Client.
   AWSCredentials credentials =
       new BasicAWSCredentials(Constants.ACCESS_KEY_ID, Constants.SECRET_KEY);
   this.sdbClient = new AmazonSimpleDBClient(credentials);
   this.sdbClient.setRegion(Region.getRegion(Regions.US_WEST_2));
   this.nextToken = null;
   this.sortMethod = sortMethod;
   this.count = -1;
 }
 /** Instantiates a new basic context. */
 public BasicChaosMonkeyContext() {
   super("simianarmy.properties", "client.properties", "chaos.properties");
   setChaosCrawler(new ASGChaosCrawler(awsClient()));
   setChaosInstanceSelector(new BasicChaosInstanceSelector());
   MonkeyConfiguration cfg = configuration();
   AmazonSimpleEmailServiceClient sesClient = new AmazonSimpleEmailServiceClient();
   if (configuration().getStr("simianarmy.aws.email.region") != null) {
     sesClient.setRegion(
         Region.getRegion(
             Regions.fromName(configuration().getStr("simianarmy.aws.email.region"))));
   }
   setChaosEmailNotifier(new BasicChaosEmailNotifier(cfg, sesClient, null));
 }
  @OnScheduled
  public void onScheduled(final ProcessContext context) {
    final ClientType awsClient =
        createClient(context, getCredentials(context), createConfiguration(context));
    this.client = awsClient;

    // if the processor supports REGION, get the configured region.
    if (getSupportedPropertyDescriptors().contains(REGION)) {
      final String region = context.getProperty(REGION).getValue();
      if (region != null) {
        client.setRegion(Region.getRegion(Regions.fromName(region)));
      }
    }
  }
Ejemplo n.º 15
0
 public static void init() {
   try {
     credentials = new ProfileCredentialsProvider("default").getCredentials();
   } catch (Exception e) {
     throw new AmazonClientException(
         "Cannot load the credentials from the credential profiles file. "
             + "Please make sure that your credentials file is at the correct "
             + "location (/Users/sandiwibowo/.aws/credentials), and is in valid format.",
         e);
   }
   AmazonDynamoDBClient dynamoDBAWS = new AmazonDynamoDBClient(credentials);
   Region usWest2 = Region.getRegion(Regions.US_WEST_2);
   dynamoDBAWS.setRegion(usWest2);
   dynamoDB = new DynamoDB(dynamoDBAWS);
 }
  public InteractionClient(
      Context context,
      AWSCredentialsProvider credentialsProvider,
      Regions region,
      InteractionConfig interactionConfig,
      ClientConfiguration clientConfiguration) {

    if (context == null) {
      throw new InvalidParameterException("Context cannot be null.");
    } else {
      this.context = context;
    }

    // Check if all necessary credentials are available.
    if (credentialsProvider == null) {
      throw new InvalidParameterException("Credentials are not set.");
    }

    if (interactionConfig == null) {
      throw new InvalidParameterException("Interaction config is not set.");
    }

    if ((interactionConfig.getUserId() == null || interactionConfig.getUserId().isEmpty())
        && !(credentialsProvider instanceof CognitoCredentialsProvider)) {
      throw new InvalidParameterException(
          "User id must be set in the config or Amazon Cognito Identity must used as the credentials provider");
    }

    this.interactionConfig = interactionConfig;
    this.credentialsProvider = credentialsProvider;
    this.interactionListener = new DefaultInteractionListener();

    // Create service low-level client.
    if (null == clientConfiguration) {
      clientConfiguration = new ClientConfiguration();
    }

    String userAgent = INTERACTION_CLIENT_USER_AGENT;

    if (!StringUtils.isBlank(clientConfiguration.getUserAgent())) {
      userAgent += clientConfiguration.getUserAgent();
    }

    clientConfiguration.setUserAgent(userAgent);

    amazonlex = new AmazonLexRuntimeClient(credentialsProvider, clientConfiguration);
    amazonlex.setRegion(Region.getRegion(region));
  }
Ejemplo n.º 17
0
  public static void main(String[] args) {
    System.out.println("Hello, World!");
    AmazonEC2Client ec2 = new AmazonEC2Client();
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest2);
    RunInstancesRequest req = new RunInstancesRequest();
    req.setImageId("ami-5189a661");
    req.setInstanceType("t2.micro");
    req.setKeyName("aws_pt-user");

    req.setMinCount(1);
    req.setMaxCount(1);

    RunInstancesResult result = ec2.runInstances(req);
    System.out.println(result.toString());
  }
  /**
   * Constructs a Lambda invoker factory object. If your Lambda function takes a long time to finish
   * (longer than the default socket timeout of 15 seconds), you can increase the timeout via {@link
   * ClientConfiguration} .
   *
   * <pre>
   * ClientConfiguration config = new ClientConfiguration();
   * config.setSocketTimeout(5 * 60 * 1000); // 5 minutes
   * LambdaInvokerFactory factory = new LambdaInvokerFactory(context, region,
   *         provider, config);
   * </pre>
   *
   * @param context context of the app. A client context will be created from the given context.
   * @param region region of Lambda service
   * @param provider a AWS credentials provider
   * @param clientConfiguration client configuration for the factory
   */
  public LambdaInvokerFactory(
      Context context,
      Regions region,
      AWSCredentialsProvider provider,
      ClientConfiguration clientConfiguration) {
    if (context == null) {
      throw new IllegalArgumentException("context can't be null");
    }
    if (provider == null) {
      throw new IllegalArgumentException("provider can't be null");
    }

    // constructs a lambda client
    lambda = new AWSLambdaClient(provider, clientConfiguration);
    lambda.setRegion(Region.getRegion(region));
    clientContext = new ClientContext(context);
  }
  @Test
  public void parseInternal_withCustomRegion_shouldConfigureDefaultClientWithCustomRegion()
      throws Exception {
    // Arrange
    DefaultListableBeanFactory registry = new DefaultListableBeanFactory();
    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(registry);

    // Act
    reader.loadBeanDefinitions(
        new ClassPathResource(getClass().getSimpleName() + "-custom-region.xml", getClass()));

    // Assert
    AmazonSNSClient amazonSns = registry.getBean(AmazonSNSClient.class);
    assertEquals(
        "https://" + Region.getRegion(Regions.EU_WEST_1).getServiceEndpoint("sns"),
        ReflectionTestUtils.getField(amazonSns, "endpoint").toString());
  }
Ejemplo n.º 20
0
  public static void main(String... args) throws IOException {
    FileSystem fileSystem = FileSystem.create(Paths.get("file/data"), 29);

    Application app = new Application();
    app.setAssetLoader(new StaticFileAssetLoader("rs2/asset"));

    // Register the default codec initializer
    app.register(new DefaultCodecInitializer());

    // Create a shutdown hook for when a stop is requested
    Runtime.getRuntime()
        .addShutdownHook(
            new Thread(
                () -> {
                  logger.info("Stop requested, shutting application down");
                  app.shutdown();
                }));

    app.use(
        new FileApplicationService(
            FileServiceBuilder.create().provider(new DiskFileProvider(fileSystem)).build()));

    app.use(new WorldApplicationService(new WorldService()));

    GameService game =
        GameServiceBuilder.create().assetLoader(new StaticFileAssetLoader("game/asset")).build();
    game.eval("require 'bootstrap'", (res) -> {});
    app.use(new GameApplicationService(game));

    AccountService accountService = new AccountService();

    // Create the amazon simple db client
    AmazonSimpleDBAsync dbClient = new AmazonSimpleDBAsyncClient();
    dbClient.setRegion(Region.getRegion(Regions.US_EAST_1));

    // Initialize the account service handler
    TransferManager transferManager = new TransferManager();
    accountService.setHandler(new AmazonAccountServiceHandler(dbClient, transferManager));

    app.use(new AccountApplicationService(accountService));

    // Set the profile serializer
    game.setProfileSerializer(new ServiceProfileSerializer(accountService));

    app.start(40001);
  }
Ejemplo n.º 21
0
 private void validateConnection(Stage.Context context, List<Stage.ConfigIssue> issues) {
   // Access Key ID - username [unique in aws]
   // secret access key - password
   AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
   s3Client = new AmazonS3Client(credentials, new ClientConfiguration());
   s3Client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));
   if (endPoint != null && !endPoint.isEmpty()) {
     s3Client.setEndpoint(endPoint);
   } else {
     s3Client.setRegion(Region.getRegion(region));
   }
   try {
     // check if the credentials are right by trying to list buckets
     s3Client.listBuckets();
   } catch (AmazonS3Exception e) {
     issues.add(
         context.createConfigIssue(
             Groups.S3.name(), "accessKeyId", Errors.S3_SPOOLDIR_20, e.toString()));
   }
 }
Ejemplo n.º 22
0
 /**
  * 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.ProfilesConfigFile
  * @see com.amazonaws.ClientConfiguration
  */
 private static void init() throws Exception {
   /*
    * The ProfileCredentialsProvider will return your [default]
    * credential profile by reading from the credentials file located at
    * (~/.aws/credentials).
    */
   AWSCredentials credentials = null;
   try {
     credentials = new ProfileCredentialsProvider().getCredentials();
   } catch (Exception e) {
     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.",
         e);
   }
   dynamoDB = new AmazonDynamoDBClient(credentials);
   Region usEast1 = Region.getRegion(Regions.US_EAST_1);
   dynamoDB.setRegion(usEast1);
 }
Ejemplo n.º 23
0
 private void setCredentials() throws C3PROException {
   if (this.sqs == null) {
     AWSCredentials credentials = null;
     try {
       System.setProperty("aws.profile", AppConfig.getProp(AppConfig.AWS_SQS_PROFILE));
       System.out.println(AppConfig.getProp(AppConfig.AWS_SQS_PROFILE));
       credentials = new ProfileCredentialsProvider().getCredentials();
     } catch (Exception e) {
       e.printStackTrace();
       throw new C3PROException(
           "Cannot load the credentials from the credential profiles file. "
               + "Please make sure that the credentials file is at the correct "
               + "location (~/.aws/credentials), and is in valid format.",
           e);
     }
     this.sqs = new AmazonSQSClient(credentials);
     System.out.println(AppConfig.getProp(AppConfig.AWS_SQS_REGION));
     Region usWest2 =
         Region.getRegion(Regions.fromName(AppConfig.getProp(AppConfig.AWS_SQS_REGION)));
     sqs.setRegion(usWest2);
   }
 }
Ejemplo n.º 24
0
  public S3NotebookRepo(ZeppelinConfiguration conf) throws IOException {
    this.conf = conf;
    bucketName = conf.getBucketName();
    user = conf.getUser();

    // always use the default provider chain
    AWSCredentialsProvider credentialsProvider = new DefaultAWSCredentialsProviderChain();
    CryptoConfiguration cryptoConf = null;
    String keyRegion = conf.getS3KMSKeyRegion();

    if (StringUtils.isNotBlank(keyRegion)) {
      cryptoConf = new CryptoConfiguration();
      cryptoConf.setAwsKmsRegion(Region.getRegion(Regions.fromName(keyRegion)));
    }

    // see if we should be encrypting data in S3
    String kmsKeyID = conf.getS3KMSKeyID();
    if (kmsKeyID != null) {
      // use the AWS KMS to encrypt data
      KMSEncryptionMaterialsProvider emp = new KMSEncryptionMaterialsProvider(kmsKeyID);
      if (cryptoConf != null) {
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp, cryptoConf);
      } else {
        this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp);
      }
    } else if (conf.getS3EncryptionMaterialsProviderClass() != null) {
      // use a custom encryption materials provider class
      EncryptionMaterialsProvider emp = createCustomProvider(conf);
      this.s3client = new AmazonS3EncryptionClient(credentialsProvider, emp);
    } else {
      // regular S3
      this.s3client = new AmazonS3Client(credentialsProvider);
    }

    // set S3 endpoint to use
    s3client.setEndpoint(conf.getEndpoint());
  }
Ejemplo n.º 25
0
  @Override
  public void sendEmail(EmailBean emailBean) {

    if (emailBean == null) {
      return;
    }

    if (CollectionUtils.isEmpty(emailBean.getRecipients())) {
      return;
    }

    final Destination destination = new Destination().withToAddresses(emailBean.getRecipients());
    final Content subjectContent = new Content().withData(emailBean.getSubject());
    final Content bodyContent = new Content().withData(emailBean.getBody());
    final Body body = new Body().withText(bodyContent);

    if (StringUtils.hasText(emailBean.getRecordsAsHtml())) {
      body.withHtml(new Content().withData(emailBean.getRecordsAsHtml()));
    }

    final Message message = new Message().withSubject(subjectContent).withBody(body);

    final SendEmailRequest request =
        new SendEmailRequest()
            .withSource(emailBean.getFrom())
            .withDestination(destination)
            .withMessage(message);

    final AmazonSimpleEmailServiceClient emailClient = new AmazonSimpleEmailServiceClient();
    final Region region = Region.getRegion(Regions.DEFAULT_REGION);
    emailClient.setRegion(region);

    SendEmailResult sendEmailResult = emailClient.sendEmail(request);

    //        TODO handle email result (Log, store state to db, etc..)
  }
Ejemplo n.º 26
0
  @Override
  // @HystrixCommand(fallback = my coole exception)
  // command for account id and client type -> generate new credentials
  public void processEvent(final CloudTrailEvent event) {

    String parameters = event.getEventData().getRequestParameters();
    String instanceId = getFromParameters(parameters);

    AmazonEC2Client client =
        getClientForAccount(
            event.getEventData().getUserIdentity().getAccountId(),
            Region.getRegion(Regions.fromName(event.getEventData().getAwsRegion())));

    DescribeInstancesRequest request = new DescribeInstancesRequest();
    request.setInstanceIds(Collections.singleton(instanceId));

    // try
    DescribeInstancesResult result = client.describeInstances(request);
    // catch credentials are old
    // throw new my coole exception ( account id, CLIENTTYPE.EC2, exception) -> this will trigger
    // hystrix

    LOG.info("SAVING RESULT INTO MAGIC DB", result);
  }
Ejemplo n.º 27
0
  /**
   * Constructs a content manager.
   *
   * @param context an Android context.
   * @param identityManager identity manager to use for credentials.
   * @param bucket the s3 bucket.
   * @param s3DirPrefix The directory within the bucket for which this content manager will manage
   *     content. This may be passed as null if the root directory of the bucket should be used. The
   *     object delimiter is always the standard directory separator of '/'.
   * @param cloudFrontDomainName The CloudFront domain name where this bucket's content may be
   *     retrieved by downloading over http from a CloudFront edge location.
   * @param basePath the base path under which to store the files managed by this content manager.
   *     This path will have a subdirectory identifying the remote location, and beneath that
   *     subdirectories 'content' and 'incoming' will be created to store the locally cached content
   *     and incoming transfers respectively.
   * @param clientConfiguration The client configuration for AWS clients.
   */
  ContentManager(
      final Context context,
      final IdentityManager identityManager,
      final String bucket,
      final String s3DirPrefix,
      final String cloudFrontDomainName,
      final String basePath,
      final ClientConfiguration clientConfiguration) {

    this.context = context.getApplicationContext();

    s3Client = new AmazonS3Client(identityManager.getCredentialsProvider(), clientConfiguration);
    s3Client.setRegion(Region.getRegion(AWSConfiguration.AMAZON_COGNITO_REGION));

    this.bucket = bucket;

    final String localDirPrefix;

    if (s3DirPrefix != null && !s3DirPrefix.isEmpty()) {
      if (s3DirPrefix.endsWith(DIR_DELIMITER)) {
        localDirPrefix = "/" + s3DirPrefix.substring(0, s3DirPrefix.length() - 1);
        this.s3DirPrefix = s3DirPrefix;
      } else {
        localDirPrefix = "/" + s3DirPrefix;
        this.s3DirPrefix = s3DirPrefix + DIR_DELIMITER;
      }
    } else {
      localDirPrefix = "";
      this.s3DirPrefix = null;
    }

    final String baseContentPath = basePath + "/s3_" + bucket + localDirPrefix;

    final File prefixPathFile = new File(baseContentPath);

    if (!prefixPathFile.exists()) {
      if (!prefixPathFile.mkdirs()) {
        throw new RuntimeException(
            String.format(
                "Can't create directory the base directory ('%s') for storing local content.",
                baseContentPath));
      }
    }

    if (!prefixPathFile.isDirectory()) {
      throw new RuntimeException(
          String.format("Prefix content path '%s' is not a directory.", baseContentPath));
    }

    localContentPath = baseContentPath + LOCAL_CONTENT_DIR_SUFFIX;
    localTransferPath = baseContentPath + LOCAL_CONTENT_XFER_DIR_SUFFIX;

    localContentCache =
        new LocalContentCache(
            context,
            "com.amazonaws.mobile.content.cache.s3." + bucket + localDirPrefix.replace("/", "."),
            localContentPath);

    if (cloudFrontDomainName == null) {
      transferHelper =
          S3TransferHelper.build(
              context, s3Client, bucket, this.s3DirPrefix, localTransferPath, localContentCache);
    } else {
      transferHelper =
          new CloudFrontTransferHelper(
              context,
              cloudFrontDomainName,
              this.s3DirPrefix,
              localTransferPath,
              localContentCache);
    }
  }
  /** @param args */
  public static void main(String[] args) {
    // ============================================================================================//
    // =============================== Submitting a Request
    // =======================================//
    // ============================================================================================//

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(new ClasspathPropertiesFileCredentialsProvider());
    Region usWest2 = Region.getRegion(Regions.US_WEST_2);
    ec2.setRegion(usWest2);

    // Initializes a Spot Instance Request
    RequestSpotInstancesRequest requestRequest = new RequestSpotInstancesRequest();

    // *************************** Required Parameters Settings ************************//
    // Request 1 x t1.micro instance with a bid price of $0.03.
    requestRequest.setSpotPrice("0.03");
    requestRequest.setInstanceCount(Integer.valueOf(1));

    // Setup the specifications of the launch. This includes the instance type (e.g. t1.micro)
    // and the latest Amazon Linux AMI id available. Note, you should always use the latest
    // Amazon Linux AMI id or another of your choosing.
    LaunchSpecification launchSpecification = new LaunchSpecification();
    launchSpecification.setImageId("ami-8c1fece5");
    launchSpecification.setInstanceType("t1.micro");

    // Add the security group to the request.
    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add("GettingStartedGroup");
    launchSpecification.setSecurityGroups(securityGroups);

    // *************************** Bid Type Settings ************************//
    // Set the type of the bid to persistent.
    requestRequest.setType("persistent");

    // *************************** Valid From/To Settings ************************//
    // Set the valid start time to be two minutes from now.
    Calendar from = Calendar.getInstance();
    from.add(Calendar.MINUTE, 2);
    requestRequest.setValidFrom(from.getTime());

    // Set the valid end time to be two minutes and two hours from now.
    Calendar until = (Calendar) from.clone();
    until.add(Calendar.HOUR, 2);
    requestRequest.setValidUntil(until.getTime());

    // *************************** Launch Group Settings ************************//
    // Set the launch group.
    requestRequest.setLaunchGroup("ADVANCED-DEMO-LAUNCH-GROUP");

    // *************************** Availability Zone Group Settings ************************//
    // Set the availability zone group.
    requestRequest.setAvailabilityZoneGroup("ADVANCED-DEMO-AZ-GROUP");

    // *************************** Add the block device mapping ************************//

    // Goal: Setup block device mappings to ensure that we will not delete
    // the root partition on termination.

    // Create the block device mapping to describe the root partition.
    BlockDeviceMapping blockDeviceMapping = new BlockDeviceMapping();
    blockDeviceMapping.setDeviceName("/dev/sda1");

    // Set the delete on termination flag to false.
    EbsBlockDevice ebs = new EbsBlockDevice();
    ebs.setDeleteOnTermination(Boolean.FALSE);
    blockDeviceMapping.setEbs(ebs);

    // Add the block device mapping to the block list.
    ArrayList<BlockDeviceMapping> blockList = new ArrayList<BlockDeviceMapping>();
    blockList.add(blockDeviceMapping);

    // Set the block device mapping configuration in the launch specifications.
    launchSpecification.setBlockDeviceMappings(blockList);

    // *************************** Add the availability zone ************************//
    // Setup the availability zone to use. Note we could retrieve the availability
    // zones using the ec2.describeAvailabilityZones() API. For this demo we will just use
    // us-east-1b.
    SpotPlacement placement = new SpotPlacement("us-east-1b");
    launchSpecification.setPlacement(placement);

    // *************************** Add the placement group ************************//
    // Setup the placement group to use with whatever name you desire.
    // For this demo we will just use "ADVANCED-DEMO-PLACEMENT-GROUP".
    // Note: We have commented this out, because we are not leveraging cc1.4xlarge or
    // cg1.4xlarge in this example.
    /*
    SpotPlacement pg = new SpotPlacement();
    pg.setGroupName("ADVANCED-DEMO-PLACEMENT-GROUP");
    launchSpecification.setPlacement(pg);
    */

    // *************************** Add the launch specification ************************//
    // Add the launch specification.
    requestRequest.setLaunchSpecification(launchSpecification);

    // ============================================================================================//
    // =========================== Getting the Request ID from the Request
    // ========================//
    // ============================================================================================//

    // Call the RequestSpotInstance API.
    RequestSpotInstancesResult requestResult = ec2.requestSpotInstances(requestRequest);
    List<SpotInstanceRequest> requestResponses = requestResult.getSpotInstanceRequests();

    // Setup an arraylist to collect all of the request ids we want to watch hit the running
    // state.
    ArrayList<String> spotInstanceRequestIds = new ArrayList<String>();

    // Add all of the request ids to the hashset, so we can determine when they hit the
    // active state.
    for (SpotInstanceRequest requestResponse : requestResponses) {
      System.out.println("Created Spot Request: " + requestResponse.getSpotInstanceRequestId());
      spotInstanceRequestIds.add(requestResponse.getSpotInstanceRequestId());
    }

    // ============================================================================================//
    // =========================== Determining the State of the Spot Request
    // ======================//
    // ============================================================================================//

    // Create a variable that will track whether there are any requests still in the open state.
    boolean anyOpen;

    // Initialize variables.
    ArrayList<String> instanceIds = new ArrayList<String>();

    do {
      // Create the describeRequest with tall of the request id to monitor (e.g. that we started).
      DescribeSpotInstanceRequestsRequest describeRequest =
          new DescribeSpotInstanceRequestsRequest();
      describeRequest.setSpotInstanceRequestIds(spotInstanceRequestIds);

      // Initialize the anyOpen variable to false ??? which assumes there are no requests open
      // unless
      // we find one that is still open.
      anyOpen = false;

      try {
        // Retrieve all of the requests we want to monitor.
        DescribeSpotInstanceRequestsResult describeResult =
            ec2.describeSpotInstanceRequests(describeRequest);
        List<SpotInstanceRequest> describeResponses = describeResult.getSpotInstanceRequests();

        // Look through each request and determine if they are all in the active state.
        for (SpotInstanceRequest describeResponse : describeResponses) {
          // If the state is open, it hasn't changed since we attempted to request it.
          // There is the potential for it to transition almost immediately to closed or
          // cancelled so we compare against open instead of active.
          if (describeResponse.getState().equals("open")) {
            anyOpen = true;
            break;
          }

          // Add the instance id to the list we will eventually terminate.
          instanceIds.add(describeResponse.getInstanceId());
        }
      } catch (AmazonServiceException e) {
        // If we have an exception, ensure we don't break out of the loop.
        // This prevents the scenario where there was blip on the wire.
        anyOpen = true;
      }

      try {
        // Sleep for 60 seconds.
        Thread.sleep(60 * 1000);
      } catch (Exception e) {
        // Do nothing because it woke up early.
      }
    } while (anyOpen);

    // ============================================================================================//
    // ====================================== Canceling the Request ==============================//
    // ============================================================================================//

    try {
      // Cancel requests.
      CancelSpotInstanceRequestsRequest cancelRequest =
          new CancelSpotInstanceRequestsRequest(spotInstanceRequestIds);
      ec2.cancelSpotInstanceRequests(cancelRequest);
    } catch (AmazonServiceException e) {
      // Write out any exceptions that may have occurred.
      System.out.println("Error cancelling instances");
      System.out.println("Caught Exception: " + e.getMessage());
      System.out.println("Reponse Status Code: " + e.getStatusCode());
      System.out.println("Error Code: " + e.getErrorCode());
      System.out.println("Request ID: " + e.getRequestId());
    }

    // ============================================================================================//
    // =================================== Terminating any Instances
    // ==============================//
    // ============================================================================================//
    try {
      // Terminate instances.
      TerminateInstancesRequest terminateRequest = new TerminateInstancesRequest(instanceIds);
      ec2.terminateInstances(terminateRequest);
    } catch (AmazonServiceException e) {
      // Write out any exceptions that may have occurred.
      System.out.println("Error terminating instances");
      System.out.println("Caught Exception: " + e.getMessage());
      System.out.println("Reponse Status Code: " + e.getStatusCode());
      System.out.println("Error Code: " + e.getErrorCode());
      System.out.println("Request ID: " + e.getRequestId());
    }
  }
Ejemplo n.º 29
0
  public static String setUpElbWithLoadGenerator(String loadGenerator) throws Exception {

    String loadBalancerName = "projecttwoelb";

    // Create the ELB client.
    elbClient = new AmazonElasticLoadBalancingClient(awsCredentials());
    Region usEast1 = Region.getRegion(Regions.US_EAST_1);
    elbClient.setRegion(usEast1);

    CreateLoadBalancerRequest elbRequest = new CreateLoadBalancerRequest();
    elbRequest.setLoadBalancerName(loadBalancerName);

    // Security Group

    DescribeSecurityGroupsRequest sgRequest = new DescribeSecurityGroupsRequest();
    ArrayList<String> list = new ArrayList<String>();
    list.add("SecurityGroupForELBAndAutoscale");
    sgRequest.setGroupNames(list);

    DescribeSecurityGroupsResult result = ec2.describeSecurityGroups(sgRequest);
    List<SecurityGroup> groups = result.getSecurityGroups();

    ArrayList<String> securityGroups = new ArrayList<String>();
    securityGroups.add(groups.get(0).getGroupId());
    elbRequest.setSecurityGroups(securityGroups);

    // Adding tags
    Collection<com.amazonaws.services.elasticloadbalancing.model.Tag> tags =
        new ArrayList<com.amazonaws.services.elasticloadbalancing.model.Tag>();

    com.amazonaws.services.elasticloadbalancing.model.Tag tag =
        new com.amazonaws.services.elasticloadbalancing.model.Tag();
    tag.setKey("Project");
    tag.setValue("2.1");
    tags.add(tag);

    AddTagsRequest tagReq = new AddTagsRequest();
    tagReq.withTags(tags);
    elbRequest.setTags(tags);

    // Specify ports
    List<Listener> listener = new ArrayList<Listener>(1);
    listener.add(new Listener("HTTP", 80, 80));
    elbRequest.setListeners(listener);

    // Speciy availability zones
    List<String> zones = new ArrayList<String>(1);
    zones.add("us-east-1c");
    elbRequest.setAvailabilityZones(zones);

    CreateLoadBalancerResult lbResult = elbClient.createLoadBalancer(elbRequest);

    // Disable Cross Zone
    ModifyLoadBalancerAttributesRequest attribRequest = new ModifyLoadBalancerAttributesRequest();
    LoadBalancerAttributes attribs = new LoadBalancerAttributes();
    CrossZoneLoadBalancing crossZone = new CrossZoneLoadBalancing();
    crossZone.setEnabled(false);
    attribs.setCrossZoneLoadBalancing(crossZone);
    attribRequest.setLoadBalancerAttributes(attribs);
    attribRequest.setLoadBalancerName(loadBalancerName);
    elbClient.modifyLoadBalancerAttributes(attribRequest);

    // Specify Health Check
    String target = "HTTP:80/heartbeat?lg=" + URLEncoder.encode(loadGenerator, "UTF-8");
    ConfigureHealthCheckRequest healthCheckRequest = new ConfigureHealthCheckRequest();
    HealthCheck healthCheck = new HealthCheck(target, 5, 2, 2, 10);
    healthCheckRequest.setHealthCheck(healthCheck);
    healthCheckRequest.setLoadBalancerName(loadBalancerName);
    elbClient.configureHealthCheck(healthCheckRequest);

    System.out.println("Load balancer created with name: " + lbResult.getDNSName());

    return lbResult.getDNSName();
  }
Ejemplo n.º 30
0
/**
 * Configuration settings for SES email service.
 *
 * @author Brad Behnke
 */
public class SESConfig extends EmailConfig {
  private String awsAccessKey;
  private String awsSecretKey;
  private Region awsRegion = Region.getRegion(Regions.US_WEST_2);
  private int sendParallelism = 2;
  private int sendBatchSize = 10;
  private int sendRetryMax = 3;

  public SESConfig() {}

  public String getAwsAccessKey() {
    return awsAccessKey;
  }

  public SESConfig awsAccessKey(String awsAccessKey) {
    this.awsAccessKey = awsAccessKey;
    return this;
  }

  public Region getAwsRegion() {
    return awsRegion;
  }

  public SESConfig awsRegion(Region awsRegion) {
    this.awsRegion = awsRegion;
    return this;
  }

  public String getAwsSecretKey() {
    return awsSecretKey;
  }

  public SESConfig awsSecretKey(String awsSecretKey) {
    this.awsSecretKey = awsSecretKey;
    return this;
  }

  public int getSendBatchSize() {
    return sendBatchSize;
  }

  public SESConfig sendBatchSize(int sendBatchSize) {
    this.sendBatchSize = sendBatchSize;
    return this;
  }

  public int getSendParallelism() {
    return sendParallelism;
  }

  public SESConfig sendParallelism(int sendParallelism) {
    this.sendParallelism = sendParallelism;
    return this;
  }

  public int getSendRetryMax() {
    return sendRetryMax;
  }

  public SESConfig sendRetryMax(int sendRetryMax) {
    this.sendRetryMax = sendRetryMax;
    return this;
  }
}