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

The following examples show how to use com.amazonaws.services.s3.model.BucketLifecycleConfiguration. 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 prepareStorageLocation(final BlobStoreConfiguration blobStoreConfiguration) {
  String bucket = getConfiguredBucket(blobStoreConfiguration);
  checkPermissions(getConfiguredBucket(blobStoreConfiguration));
  if (!s3.doesBucketExistV2(bucket)) {
    try {
      s3.createBucket(bucket);
    }
    catch (AmazonS3Exception e) {
      if (ACCESS_DENIED_CODE.equals(e.getErrorCode())) {
        log.debug("Error creating bucket {}", bucket, e);
        throw insufficientCreatePermissionsError();
      }
      log.info("Error creating bucket {}", bucket, e);
      throw unexpectedError("creating bucket");
    }
    setBucketLifecycleConfiguration(s3, blobStoreConfiguration, null);
  }
  else {
    // bucket exists, we should test that the correct lifecycle config is present
    BucketLifecycleConfiguration lifecycleConfiguration = s3.getBucketLifecycleConfiguration(bucket);
    if (!isExpirationLifecycleConfigurationPresent(lifecycleConfiguration, blobStoreConfiguration)) {
      setBucketLifecycleConfiguration(s3, blobStoreConfiguration, lifecycleConfiguration);
    }
  }
}
 
Example #2
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 #3
Source File: S3BlobStore.java    From nexus-blobstore-s3 with Eclipse Public License 1.0 5 votes vote down vote up
boolean isExpirationLifecycleConfigurationPresent(BucketLifecycleConfiguration lifecycleConfiguration) {
  return lifecycleConfiguration != null &&
      lifecycleConfiguration.getRules() != null &&
      lifecycleConfiguration.getRules().stream()
      .filter(r -> r.getExpirationInDays() == getConfiguredExpirationInDays())
      .filter(r -> {
        LifecycleFilterPredicate predicate = r.getFilter().getPredicate();
        if (predicate instanceof LifecycleTagPredicate) {
          LifecycleTagPredicate tagPredicate = (LifecycleTagPredicate) predicate;
          return DELETED_TAG.equals(tagPredicate.getTag());
        }
        return false;
      })
      .findAny().isPresent();
}
 
Example #4
Source File: S3BlobStore.java    From nexus-blobstore-s3 with Eclipse Public License 1.0 5 votes vote down vote up
BucketLifecycleConfiguration makeLifecycleConfiguration(BucketLifecycleConfiguration existing, int expirationInDays) {
  BucketLifecycleConfiguration.Rule rule = new BucketLifecycleConfiguration.Rule()
      .withId(LIFECYCLE_EXPIRATION_RULE_ID)
      .withFilter(new LifecycleFilter(
          new LifecycleTagPredicate(DELETED_TAG)))
      .withExpirationInDays(expirationInDays)
      .withStatus(BucketLifecycleConfiguration.ENABLED.toString());

  if (existing != null) {
    existing.getRules().add(rule);
    return existing;
  } else {
    return new BucketLifecycleConfiguration().withRules(rule);
  }
}
 
Example #5
Source File: S3BlobStore.java    From nexus-blobstore-s3 with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public void init(final BlobStoreConfiguration configuration) {
  this.blobStoreConfiguration = configuration;
  try {
    this.s3 = amazonS3Factory.create(configuration);
    if (!s3.doesBucketExist(getConfiguredBucket())) {
      s3.createBucket(getConfiguredBucket());

      if (getConfiguredExpirationInDays()>=0) {
        addBucketLifecycleConfiguration(null);
      }
    } else {
      if (getConfiguredExpirationInDays()>=0) {
        // bucket exists, we should test that the correct lifecycle config is present
        BucketLifecycleConfiguration lifecycleConfiguration = s3.getBucketLifecycleConfiguration(getConfiguredBucket());
        if (!isExpirationLifecycleConfigurationPresent(lifecycleConfiguration)) {
          addBucketLifecycleConfiguration(lifecycleConfiguration);
        }
      }
    }

    setConfiguredBucket(getConfiguredBucket());
  }
  catch (Exception e) {
    throw new BlobStoreException("Unable to initialize blob store bucket: " + getConfiguredBucket(), e, null);
  }
}
 
Example #6
Source File: BucketManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean isExpirationLifecycleConfigurationPresent(final BucketLifecycleConfiguration lifecycleConfiguration,
                                                          final BlobStoreConfiguration blobStoreConfiguration) {
  String bucketPrefix = getBucketPrefix(blobStoreConfiguration);
  int expirationInDays = getConfiguredExpirationInDays(blobStoreConfiguration);
  return lifecycleConfiguration != null &&
      lifecycleConfiguration.getRules() != null &&
      lifecycleConfiguration.getRules().stream()
      .filter(r -> r.getExpirationInDays() == expirationInDays)
      .anyMatch(r -> isDeletedTagPredicate(r.getFilter().getPredicate(), bucketPrefix));
}
 
Example #7
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 #8
Source File: BucketManager.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private void setBucketLifecycleConfiguration(final AmazonS3 s3,
                                             final BlobStoreConfiguration blobStoreConfiguration,
                                             final BucketLifecycleConfiguration lifecycleConfiguration) {
  String bucket = getConfiguredBucket(blobStoreConfiguration);
  BucketLifecycleConfiguration newLifecycleConfiguration =
      makeLifecycleConfiguration(lifecycleConfiguration, blobStoreConfiguration);
  if (newLifecycleConfiguration != null) {
    s3.setBucketLifecycleConfiguration(bucket, newLifecycleConfiguration);
  }
  else {
    s3.deleteBucketLifecycleConfiguration(bucket);
  }
}
 
Example #9
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 #10
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void setBucketLifecycleConfiguration(String bucketName,
    BucketLifecycleConfiguration bucketLifecycleConfiguration) {
  // TODO Auto-generated method stub

}
 
Example #11
Source File: AmazonS3Mock.java    From Scribengin with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) {
  // TODO Auto-generated method stub
  return null;
}
 
Example #12
Source File: BucketTransition.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void run() {
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration());
    if (endpoint.contains("amazonaws.com")) {
        String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
        if (aws_endpoint.contains("US")) {
            s3Client.setEndpoint("https://s3.amazonaws.com");
        } else if (aws_endpoint.contains("us-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("eu-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("ap-")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("sa-east-1")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else {
            s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
        }
    } else {
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
    }
    int converted_days = 0;
    if (!disabled) {
        converted_days = Integer.parseInt(days);
    }
    BucketLifecycleConfiguration.Rule ruleArchiveAndExpire = null;
    if (!disabled) {
        ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule()
                .withPrefix(prefix)
                .withExpirationInDays(converted_days)
                .withStatus(BucketLifecycleConfiguration.ENABLED.toString());
    } else {
        ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule()
                .withPrefix(prefix)
                .withExpirationInDays(100)
                .withStatus(BucketLifecycleConfiguration.DISABLED.toString());
    }
    List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
    rules.add(ruleArchiveAndExpire);

    try {
        BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration()
                .withRules(rules);
        s3Client.setBucketLifecycleConfiguration(bucket, configuration);
    } catch (Exception get) {
        mainFrame.jTextArea1.append("\n" + get.getMessage());
    }
    if (!disabled) {
        mainFrame.jTextArea1.append("\nSent request to change bucket life cycle to " + converted_days + " day(s). Please observe for any errors.");
    } else {
        mainFrame.jTextArea1.append("\nSent request to disable the bucket life cycle. Please observe for any errors.");
    }
    calibrate();
    mainFrame.jPanel9.setVisible(true);
}
 
Example #13
Source File: BucketTransitionGlacier.java    From cloudExplorer with GNU General Public License v3.0 4 votes vote down vote up
public void run() {
    AWSCredentials credentials = new BasicAWSCredentials(access_key, secret_key);
    AmazonS3 s3Client = new AmazonS3Client(credentials,
            new ClientConfiguration());
    if (endpoint.contains("amazonaws.com")) {
        String aws_endpoint = s3Client.getBucketLocation(new GetBucketLocationRequest(bucket));
        if (aws_endpoint.contains("US")) {
            s3Client.setEndpoint("https://s3.amazonaws.com");
        } else if (aws_endpoint.contains("us-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("eu-west")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("ap-")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else if (aws_endpoint.contains("sa-east-1")) {
            s3Client.setEndpoint("https://s3-" + aws_endpoint + ".amazonaws.com");
        } else {
            s3Client.setEndpoint("https://s3." + aws_endpoint + ".amazonaws.com");
        }
    } else {
        s3Client.setS3ClientOptions(S3ClientOptions.builder().setPathStyleAccess(true).build());
        s3Client.setEndpoint(endpoint);
    }
    int converted_days = 0;
    if (!disabled) {
        converted_days = Integer.parseInt(days);
    }

    Transition transToArchive = new Transition()
            .withDays(converted_days)
            .withStorageClass(StorageClass.Glacier);

    BucketLifecycleConfiguration.Rule ruleArchiveAndExpire = null;
    if (!disabled) {
        ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule()
                .withPrefix(prefix)
                .withTransition(transToArchive)
                // .withExpirationInDays(converted_days + 1)
                .withStatus(BucketLifecycleConfiguration.ENABLED.toString());
    } else {
        ruleArchiveAndExpire = new BucketLifecycleConfiguration.Rule()
                .withPrefix(prefix)
                .withTransition(transToArchive)
                //.withExpirationInDays(100)
                .withStatus(BucketLifecycleConfiguration.DISABLED.toString());
    }
    List<BucketLifecycleConfiguration.Rule> rules = new ArrayList<BucketLifecycleConfiguration.Rule>();
    rules.add(ruleArchiveAndExpire);

    try {
        BucketLifecycleConfiguration configuration = new BucketLifecycleConfiguration()
                .withRules(rules);
        s3Client.setBucketLifecycleConfiguration(bucket, configuration);
    } catch (Exception get) {
        mainFrame.jTextArea1.append("\n" + get.getMessage());
    }
    if (!disabled) {
        mainFrame.jTextArea1.append("\nSent request to set bucket life cycle to tier to Glacier after: " + converted_days + " day(s). Please observe for any errors.");
    } else {
        mainFrame.jTextArea1.append("\nSent request to disable the bucket life cycle. Please observe for any errors.");
    }
    calibrate();
}
 
Example #14
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public void setBucketLifecycleConfiguration(String bucketName,
    BucketLifecycleConfiguration bucketLifecycleConfiguration) {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #15
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(
    GetBucketLifecycleConfigurationRequest getBucketLifecycleConfigurationReq) {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #16
Source File: DummyS3Client.java    From ignite with Apache License 2.0 4 votes vote down vote up
/** Unsupported Operation. */
@Override public BucketLifecycleConfiguration getBucketLifecycleConfiguration(String bucketName) {
    throw new UnsupportedOperationException("Operation not supported");
}
 
Example #17
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;
  }
 
Example #18
Source File: S3BlobStore.java    From nexus-blobstore-s3 with Eclipse Public License 1.0 4 votes vote down vote up
private void addBucketLifecycleConfiguration(BucketLifecycleConfiguration lifecycleConfiguration) {
  s3.setBucketLifecycleConfiguration(
      getConfiguredBucket(),
      makeLifecycleConfiguration(lifecycleConfiguration, getConfiguredExpirationInDays()));
}