com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule Java Examples

The following examples show how to use com.amazonaws.services.s3.model.BucketLifecycleConfiguration.Rule. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BucketManager.java    From nexus-public with Eclipse Public License 1.0 6 votes vote down vote up
@Override
public void deleteStorageLocation(final BlobStoreConfiguration blobStoreConfiguration) {
  String bucket = getConfiguredBucket(blobStoreConfiguration);
  ObjectListing listing = s3.listObjects(new ListObjectsRequest().withBucketName(bucket).withMaxKeys(1));
  if (listing.getObjectSummaries().isEmpty()) {
    s3.deleteBucket(bucket);
  }
  else {
    log.info("Not removing S3 bucket {} because it is not empty", bucket);
    BucketLifecycleConfiguration lifecycleConfiguration = s3.getBucketLifecycleConfiguration(bucket);
    List<Rule> nonBlobstoreRules = nonBlobstoreRules(lifecycleConfiguration, blobStoreConfiguration.getName());
    if(!isEmpty(nonBlobstoreRules)) {
      lifecycleConfiguration.setRules(nonBlobstoreRules);
      s3.setBucketLifecycleConfiguration(bucket, lifecycleConfiguration);
    } else {
      s3.deleteBucketLifecycleConfiguration(bucket);
    }
  }
}
 
Example #2
Source File: BucketManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private List<Rule> nonBlobstoreRules(final BucketLifecycleConfiguration existing, final String blobStoreName) {
  List<Rule> rules = existing.getRules();
  if (rules == null) {
    return emptyList();
  }
  return rules.stream()
      .filter(r -> !r.getId().equals(LIFECYCLE_EXPIRATION_RULE_ID_PREFIX + blobStoreName) &&
              !r.getId().equals(OLD_LIFECYCLE_EXPIRATION_RULE_ID))
      .collect(toList());
}
 
Example #3
Source File: AmazonInitializer.java    From entrada with GNU General Public License v3.0 5 votes vote down vote up
private BucketLifecycleConfiguration.Rule createExpirationRule(String id, String prefix,
    int expiration) {
  return new BucketLifecycleConfiguration.Rule()
      .withId(id)
      .withFilter(new LifecycleFilter(new LifecyclePrefixPredicate(prefix)))
      .withExpirationInDays(expiration)
      .withStatus(BucketLifecycleConfiguration.ENABLED);
}
 
Example #4
Source File: XmlResponsesSaxParser.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
protected void doStartElement(
    String uri,
    String name,
    String qName,
    Attributes attrs) {

    if (in("LifecycleConfiguration")) {
        if (name.equals("Rule")) {
            currentRule = new Rule();
        }
    } else if (in("LifecycleConfiguration", "Rule")) {
        if (name.equals("Transition")) {
            currentTransition = new Transition();
        } else if (name.equals("NoncurrentVersionTransition")) {
            currentNcvTransition = new NoncurrentVersionTransition();
        } else if (name.equals("AbortIncompleteMultipartUpload")) {
            abortIncompleteMultipartUpload = new
                AbortIncompleteMultipartUpload();
        } else if (name.equals("Filter")) {
            currentFilter = new LifecycleFilter();
        }
    } else if (in("LifecycleConfiguration", "Rule", "Filter")) {
        if (name.equals("And")) {
            andOperandsList = new ArrayList<LifecycleFilterPredicate>();
        }
    }
}
 
Example #5
Source File: AmazonInitializer.java    From entrada with GNU General Public License v3.0 4 votes vote down vote up
private boolean enableBucketLifecycle(String location, String prefix, int exp, boolean optional) {

    if (optional && !fileManager.supported(location)) {
      // location not a s3 location, but it is an optional policy so not a problem
      // do nothing
      return true;
    }

    Optional<S3Details> s3Loc = S3Details.from(location);
    if (s3Loc.isPresent()) {

      BucketLifecycleConfiguration cfg = amazonS3.getBucketLifecycleConfiguration(bucket);
      if (cfg == null) {
        // no config found, ceate a new config
        cfg = new BucketLifecycleConfiguration().withRules(new ArrayList<>());
      }
      Optional<Rule> oldRule = cfg
          .getRules()
          .stream()
          .filter(r -> StringUtils.startsWithIgnoreCase(r.getId(), prefix))
          .findFirst();

      if (!oldRule.isPresent()) {
        // no rule found found create new
        cfg
            .getRules()
            .add(createExpirationRule(prefix + " after " + exp + " day(s)", s3Loc.get().getKey(),
                exp));
        // Save the configuration.
        amazonS3.setBucketLifecycleConfiguration(bucket, cfg);
        return true;
      } else {
        // existing rule found, check if need to update
        if (oldRule.get().getExpirationInDays() != exp) {
          log
              .info(
                  "Lifecycle policy rule with prefix: '{}' has changed from {} to {}, creating new policy rule.",
                  prefix, oldRule.get().getExpirationInDays(), exp);
          // remove old rule and add new rule
          cfg.getRules().remove(oldRule.get());
          cfg
              .getRules()
              .add(createExpirationRule(prefix + " after " + exp + " day(s)", s3Loc.get().getKey(),
                  exp));

          amazonS3.setBucketLifecycleConfiguration(bucket, cfg);
        }
        return true;
      }
    }
    return false;
  }