简体   繁体   中英

Deleting an AWS CloudFront distribution using Java API

I've written some Scala code that uses the Java API to create and delete CloudFront distributions.

The code in question is the removeDistribution method . Output is:

Disabling distribution of scalacoursesdemo.s3.amazonaws.com with id E3656VVNINIXI3 and ETag E3W3IUM6RJKTDY
distributionConfig={
  "enabled" : false,
  "restrictions" : {
    "geoRestriction" : {
      "restrictionType" : "none",
      "items" : [ ],
      "quantity" : 0
    }
  },
  "priceClass" : "PriceClass_All",
  "defaultRootObject" : "index.html",
  "logging" : {
    "enabled" : false,
    "bucket" : "",
    "prefix" : "",
    "includeCookies" : false
  },
  "customErrorResponses" : {
    "items" : [ ],
    "quantity" : 0
  },
  "aliases" : {
    "items" : [ ],
    "quantity" : 0
  },
  "cacheBehaviors" : {
    "items" : [ ],
    "quantity" : 0
  },
  "origins" : {
    "items" : [ {
      "id" : "S3-scalacoursesdemo",
      "customOriginConfig" : null,
      "s3OriginConfig" : {
        "originAccessIdentity" : ""
      },
      "domainName" : "scalacoursesdemo.s3.amazonaws.com"
    } ],
    "quantity" : 1
  },
  "comment" : "",
  "callerReference" : "284851146697",
  "defaultCacheBehavior" : {
    "targetOriginId" : "S3-scalacoursesdemo",
    "minTTL" : 3600,
    "viewerProtocolPolicy" : "allow-all",
    "trustedSigners" : {
      "enabled" : false,
      "items" : [ ],
      "quantity" : 0
    },
    "allowedMethods" : {
      "items" : [ "GET", "HEAD" ],
      "quantity" : 2
    },
    "smoothStreaming" : false,
    "forwardedValues" : {
      "queryString" : false,
      "cookies" : {
        "forward" : "all",
        "whitelistedNames" : null
      }
    }
  },
  "viewerCertificate" : {
    "SSLSupportMethod" : null,
    "cloudFrontDefaultCertificate" : true,
    "IAMCertificateId" : null
  }
}
Update result ETag = E14HGEUEBH4TKO
Deleting distribution of scalacoursesdemo.s3.amazonaws.com with id E3656VVNINIXI3 and ETag E14HGEUEBH4TKO.
deleteDistributionRequest={
  "id" : "E3656VVNINIXI3",
  "requestClientOptions" : {
    "clientMarker" : null
  },
  "ifMatch" : "E14HGEUEBH4TKO",
  "requestMetricCollector" : null,
  "requestCredentials" : null
}

Distribution of scalacoursesdemo.s3.amazonaws.com with id 'E3656VVNINIXI3' and ETag 'E3W3IUM6RJKTDY': 
The distribution you are trying to delete has not been disabled. 
(Service: AmazonCloudFront; Status Code: 409; Error Code: DistributionNotDisabled; Request ID: ab6e9920-d3aa-11e3-a498-7f333cd2eda0)

Problem is that the AWS web client DOES show the distribution as being disabled. If I then try to use CloudBerry, it is also unable to delete the distribution. An hour or so later, the problem goes away and CloudBerry is able to delete the distribution.

If I rerun my program on another distribution, created by CloudBerry, it disables the distribution, then generates this error:

The precondition given in one or more of the request-header fields evaluated to false.

That error message is most unhelpful. I can see it listed here . I think it means that the ifMatch ETag is incorrect. Not sure where else to get that value from.

BTW, I think I have found an error in the CloudFront docs . I believe it is more correct to say that the If-Match value should be set from the eTag from the most recent GET or PUT operation, not The value of the ETag header you received when you disabled the distribution .

I tried waiting 3 seconds after creating the distribution, and also waiting 3 seconds between disabling it before deleting it, but that did not help.

Obviously something is wrong with my program, but I don't see it. Suggestions?

Found the fix to this, in the return from your GET request (getDistribution) There will be an 'ETag' field, pass that along to your removeDistribution request as 'IfMatch' and it will successfully go through. I am using node with aws-sdk, so it looked like this:

aws.removeDistribution({
    Id: result.Id,
    IfMatch: result.ETag,  
}, function(err, response){console.log(err, response)});

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM