Search in sources :

Example 1 with LifecycleConfig

use of org.jets3t.service.model.LifecycleConfig in project cyberduck by iterate-ch.

the class S3LifecycleConfiguration method setConfiguration.

@Override
public void setConfiguration(final Path file, final LifecycleConfiguration configuration) throws BackgroundException {
    final Path bucket = containerService.getContainer(file);
    try {
        if (configuration.getTransition() != null || configuration.getExpiration() != null) {
            final LifecycleConfig config = new LifecycleConfig();
            // Unique identifier for the rule. The value cannot be longer than 255 characters. When you specify an empty prefix, the rule applies to all objects in the bucket
            final LifecycleConfig.Rule rule = config.newRule(String.format("%s-%s", PreferencesFactory.get().getProperty("application.name"), new AlphanumericRandomStringService().random()), StringUtils.EMPTY, true);
            if (configuration.getTransition() != null) {
                final LifecycleConfig.Transition transition = rule.newTransition();
                transition.setDays(configuration.getTransition());
                transition.setStorageClass(new HostPreferences(session.getHost()).getProperty("s3.lifecycle.transition.class"));
            }
            if (configuration.getExpiration() != null) {
                final LifecycleConfig.Expiration expiration = rule.newExpiration();
                expiration.setDays(configuration.getExpiration());
            }
            session.getClient().setLifecycleConfig(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName(), config);
        } else {
            session.getClient().deleteLifecycleConfig(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName());
        }
    } catch (ServiceException e) {
        throw new S3ExceptionMappingService().map("Failure to write attributes of {0}", e, bucket);
    }
}
Also used : Path(ch.cyberduck.core.Path) LifecycleConfig(org.jets3t.service.model.LifecycleConfig) ServiceException(org.jets3t.service.ServiceException) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) HostPreferences(ch.cyberduck.core.preferences.HostPreferences)

Example 2 with LifecycleConfig

use of org.jets3t.service.model.LifecycleConfig in project cyberduck by iterate-ch.

the class S3LifecycleConfiguration method getConfiguration.

@Override
public LifecycleConfiguration getConfiguration(final Path file) throws BackgroundException {
    final Path bucket = containerService.getContainer(file);
    if (file.getType().contains(Path.Type.upload)) {
        return LifecycleConfiguration.empty();
    }
    try {
        final LifecycleConfig status = session.getClient().getLifecycleConfig(bucket.isRoot() ? StringUtils.EMPTY : bucket.getName());
        if (null == status) {
            log.warn(String.format("Failure parsing lifecycle config for %s", bucket));
            return LifecycleConfiguration.empty();
        }
        Integer transition = null;
        Integer expiration = null;
        String storageClass = null;
        for (LifecycleConfig.Rule rule : status.getRules()) {
            if (StringUtils.isBlank(rule.getPrefix())) {
                if (rule.getTransition() != null) {
                    transition = rule.getTransition().getDays();
                }
                if (rule.getExpiration() != null) {
                    expiration = rule.getExpiration().getDays();
                }
            }
        }
        return new LifecycleConfiguration(transition, expiration);
    } catch (ServiceException e) {
        try {
            throw new S3ExceptionMappingService().map("Failure to read attributes of {0}", e, bucket);
        } catch (AccessDeniedException | InteroperabilityException l) {
            log.warn(String.format("Missing permission to read lifecycle configuration for %s %s", bucket, e.getMessage()));
            return LifecycleConfiguration.empty();
        }
    }
}
Also used : Path(ch.cyberduck.core.Path) LifecycleConfig(org.jets3t.service.model.LifecycleConfig) ServiceException(org.jets3t.service.ServiceException) LifecycleConfiguration(ch.cyberduck.core.lifecycle.LifecycleConfiguration)

Aggregations

Path (ch.cyberduck.core.Path)2 ServiceException (org.jets3t.service.ServiceException)2 LifecycleConfig (org.jets3t.service.model.LifecycleConfig)2 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)1 LifecycleConfiguration (ch.cyberduck.core.lifecycle.LifecycleConfiguration)1 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)1