Search in sources :

Example 11 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method createCustomDistribution.

protected com.amazonaws.services.cloudfront.model.Distribution createCustomDistribution(final Path container, final Distribution distribution) throws BackgroundException {
    final AmazonCloudFront client = this.client(container);
    int httpPort = 80;
    int httpsPort = 443;
    final URI origin = this.getOrigin(container, distribution.getMethod());
    if (origin.getPort() != -1) {
        if (origin.getScheme().equals(Scheme.http.name())) {
            httpPort = origin.getPort();
        }
        if (origin.getScheme().equals(Scheme.https.name())) {
            httpsPort = origin.getPort();
        }
    }
    final String originId = String.format("%s-%s", preferences.getProperty("application.name"), new AlphanumericRandomStringService().random());
    final DistributionConfig config = new DistributionConfig(new AlphanumericRandomStringService().random(), distribution.isEnabled()).withComment(originId).withOrigins(new Origins().withQuantity(1).withItems(new Origin().withId(originId).withDomainName(origin.getHost()).withCustomOriginConfig(new CustomOriginConfig().withHTTPPort(httpPort).withHTTPSPort(httpsPort).withOriginSslProtocols(new OriginSslProtocols().withQuantity(2).withItems("TLSv1.1", "TLSv1.2")).withOriginProtocolPolicy(this.getPolicy(distribution.getMethod()))))).withPriceClass(PriceClass.PriceClass_All).withDefaultCacheBehavior(new DefaultCacheBehavior().withTargetOriginId(originId).withForwardedValues(new ForwardedValues().withQueryString(true).withCookies(new CookiePreference().withForward(ItemSelection.All))).withViewerProtocolPolicy(ViewerProtocolPolicy.AllowAll).withMinTTL(0L).withTrustedSigners(new TrustedSigners().withEnabled(false).withQuantity(0))).withDefaultRootObject(distribution.getIndexDocument()).withAliases(new Aliases().withItems(distribution.getCNAMEs()).withQuantity(distribution.getCNAMEs().length));
    if (distribution.isLogging()) {
        // Make bucket name fully qualified
        final String loggingTarget = ServiceUtils.generateS3HostnameForBucket(distribution.getLoggingContainer(), false, new S3Protocol().getDefaultHostname());
        if (log.isDebugEnabled()) {
            log.debug(String.format("Set logging target for %s to %s", distribution, loggingTarget));
        }
        config.setLogging(new LoggingConfig().withEnabled(distribution.isLogging()).withIncludeCookies(true).withBucket(loggingTarget).withPrefix(new HostPreferences(session.getHost()).getProperty("cloudfront.logging.prefix")));
    }
    return client.createDistribution(new CreateDistributionRequest(config)).getDistribution();
}
Also used : URI(java.net.URI) S3Protocol(ch.cyberduck.core.s3.S3Protocol) HostPreferences(ch.cyberduck.core.preferences.HostPreferences) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront)

Example 12 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method invalidate.

/**
 * You can make any number of invalidation requests, but you can have only three invalidation requests in progress
 * at one time. Each request can contain up to 1,000 objects to invalidate. If you exceed these limits, you get an
 * error message.
 * <p>
 * It usually takes 10 to 15 minutes to complete your invalidation request, depending on the size of your request.
 */
@Override
public void invalidate(final Path container, final Distribution.Method method, final List<Path> files, final LoginCallback prompt) throws BackgroundException {
    try {
        final Distribution d = this.read(container, method, prompt);
        if (d.isEnabled()) {
            final List<String> keys = new ArrayList<>();
            for (Path file : files) {
                if (session.getFeature(PathContainerService.class).isContainer(file)) {
                    // To invalidate all of the objects in a distribution
                    keys.add(String.format("%s*", Path.DELIMITER));
                } else {
                    if (file.isDirectory()) {
                        // The *, which replaces 0 or more characters, must be the last character in the invalidation path
                        keys.add(String.format("/%s*", session.getFeature(PathContainerService.class).getKey(file)));
                    } else {
                        keys.add(String.format("/%s", session.getFeature(PathContainerService.class).getKey(file)));
                    }
                }
            }
            if (keys.isEmpty()) {
                log.warn("No keys selected for invalidation");
            } else {
                final AmazonCloudFront client = this.client(container);
                client.createInvalidation(new CreateInvalidationRequest(d.getId(), new InvalidationBatch(new Paths().withItems(keys).withQuantity(keys.size()), new AlphanumericRandomStringService().random())));
            }
        }
    } catch (AmazonClientException e) {
        throw new AmazonServiceExceptionMappingService().map("Cannot write CDN configuration", e);
    }
}
Also used : Path(ch.cyberduck.core.Path) AmazonClientException(com.amazonaws.AmazonClientException) ArrayList(java.util.ArrayList) AmazonServiceExceptionMappingService(ch.cyberduck.core.aws.AmazonServiceExceptionMappingService) PathContainerService(ch.cyberduck.core.PathContainerService) Distribution(ch.cyberduck.core.cdn.Distribution) AlphanumericRandomStringService(ch.cyberduck.core.AlphanumericRandomStringService) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront)

Example 13 with AmazonCloudFront

use of com.amazonaws.services.cloudfront.AmazonCloudFront in project cyberduck by iterate-ch.

the class CloudFrontDistributionConfiguration method deleteStreamingDistribution.

protected void deleteStreamingDistribution(final Path container, final Distribution distribution) throws BackgroundException {
    final URI origin = this.getOrigin(container, distribution.getMethod());
    if (log.isDebugEnabled()) {
        log.debug(String.format("Update %s distribution with origin %s", distribution, origin));
    }
    final AmazonCloudFront client = this.client(container);
    client.deleteStreamingDistribution(new DeleteStreamingDistributionRequest(distribution.getId(), distribution.getEtag()));
}
Also used : AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) URI(java.net.URI)

Aggregations

AmazonCloudFront (com.amazonaws.services.cloudfront.AmazonCloudFront)13 URI (java.net.URI)9 HostPreferences (ch.cyberduck.core.preferences.HostPreferences)6 S3Protocol (ch.cyberduck.core.s3.S3Protocol)6 AlphanumericRandomStringService (ch.cyberduck.core.AlphanumericRandomStringService)4 Path (ch.cyberduck.core.Path)2 PathContainerService (ch.cyberduck.core.PathContainerService)2 AmazonServiceExceptionMappingService (ch.cyberduck.core.aws.AmazonServiceExceptionMappingService)2 Distribution (ch.cyberduck.core.cdn.Distribution)2 AmazonClientException (com.amazonaws.AmazonClientException)2 SdkClientException (com.amazonaws.SdkClientException)1 Policy (com.amazonaws.auth.policy.Policy)1 Principal (com.amazonaws.auth.policy.Principal)1 Statement (com.amazonaws.auth.policy.Statement)1 S3Actions (com.amazonaws.auth.policy.actions.S3Actions)1 S3ObjectResource (com.amazonaws.auth.policy.resources.S3ObjectResource)1 com.amazonaws.services.cloudfront.model (com.amazonaws.services.cloudfront.model)1 CreateInvalidationRequest (com.amazonaws.services.cloudfront.model.CreateInvalidationRequest)1 CreateInvalidationResult (com.amazonaws.services.cloudfront.model.CreateInvalidationResult)1 InvalidationBatch (com.amazonaws.services.cloudfront.model.InvalidationBatch)1