public void InvalidateObject(string bucket, string key) { var distId = GetDistributionIdFor(bucket); if (!string.IsNullOrWhiteSpace(distId)) { var preparedKey = key.StartsWith("/") ? key : "/" + key; lock(pendingRequestLockTarget) { if (pendingRequest == null) { pendingRequest = new CreateInvalidationRequest() { DistributionId = distId, InvalidationBatch = new InvalidationBatch() { CallerReference = DateTime.Now.ToString(dateFormatWithMilliseconds), Paths = new Paths { Quantity = 1, Items = new List<string> {preparedKey} } } }; } else { pendingRequest.InvalidationBatch.Paths.Items.Add(preparedKey); } } } }
public void Flush() { lock(pendingRequestLockTarget) { if (pendingRequest != null) { cloudFrontClient.CreateInvalidation(pendingRequest); pendingRequest = null; } } }
/// <summary> /// Initiates the asynchronous execution of the CreateInvalidation operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateInvalidation operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public Task<CreateInvalidationResponse> CreateInvalidationAsync(CreateInvalidationRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.Instance; return InvokeAsync<CreateInvalidationRequest,CreateInvalidationResponse>(request, marshaller, unmarshaller, cancellationToken); }
/// <summary> /// Create a new invalidation. /// </summary> /// <param name="request">Container for the necessary parameters to execute the CreateInvalidation service method.</param> /// /// <returns>The response from the CreateInvalidation service method, as returned by CloudFront.</returns> /// <exception cref="AccessDeniedException"> /// Access denied. /// </exception> /// <exception cref="BatchTooLargeException"> /// /// </exception> /// <exception cref="InconsistentQuantitiesException"> /// The value of Quantity and the size of Items do not match. /// </exception> /// <exception cref="InvalidArgumentException"> /// The argument is invalid. /// </exception> /// <exception cref="MissingBodyException"> /// This operation requires a body. Ensure that the body is present and the Content-Type /// header is set. /// </exception> /// <exception cref="NoSuchDistributionException"> /// The specified distribution does not exist. /// </exception> /// <exception cref="TooManyInvalidationsInProgressException"> /// You have exceeded the maximum number of allowable InProgress invalidation batch requests, /// or invalidation objects. /// </exception> public CreateInvalidationResponse CreateInvalidation(CreateInvalidationRequest request) { var task = CreateInvalidationAsync(request); try { return task.Result; } catch(AggregateException e) { ExceptionDispatchInfo.Capture(e.InnerException).Throw(); return null; } }
/// <summary> /// <para> Create a new invalidation. </para> /// </summary> /// /// <param name="createInvalidationRequest">Container for the necessary parameters to execute the CreateInvalidation service method on /// AmazonCloudFront.</param> /// /// <returns>The response from the CreateInvalidation service method, as returned by AmazonCloudFront.</returns> /// /// <exception cref="TooManyInvalidationsInProgressException"/> /// <exception cref="MissingBodyException"/> /// <exception cref="NoSuchDistributionException"/> /// <exception cref="BatchTooLargeException"/> /// <exception cref="AccessDeniedException"/> /// <exception cref="InvalidArgumentException"/> /// <exception cref="InconsistentQuantitiesException"/> public CreateInvalidationResponse CreateInvalidation(CreateInvalidationRequest createInvalidationRequest) { IAsyncResult asyncResult = invokeCreateInvalidation(createInvalidationRequest, null, null, true); return EndCreateInvalidation(asyncResult); }
/// <summary> /// Initiates the asynchronous execution of the CreateInvalidation operation. /// <seealso cref="Amazon.CloudFront.AmazonCloudFront.CreateInvalidation"/> /// </summary> /// /// <param name="createInvalidationRequest">Container for the necessary parameters to execute the CreateInvalidation operation on /// AmazonCloudFront.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking /// EndCreateInvalidation operation.</returns> public IAsyncResult BeginCreateInvalidation(CreateInvalidationRequest createInvalidationRequest, AsyncCallback callback, object state) { return invokeCreateInvalidation(createInvalidationRequest, callback, state, false); }
IAsyncResult invokeCreateInvalidation(CreateInvalidationRequest createInvalidationRequest, AsyncCallback callback, object state, bool synchronized) { IRequest irequest = new CreateInvalidationRequestMarshaller().Marshall(createInvalidationRequest); var unmarshaller = CreateInvalidationResponseUnmarshaller.GetInstance(); AsyncResult result = new AsyncResult(irequest, callback, state, synchronized, signer, unmarshaller); Invoke(result); return result; }
/// <summary> /// Invalidates objects from a CloudFront distribution. /// </summary> /// <param name="distributionId">The distribution to invalidate objects from.</param> /// <param name="items">The path of the objects to invalidate.</param> /// <param name="reference">A unique name that ensures the request can't be replayed.</param> /// <param name="settings">The <see cref="CloudFrontSettings"/> required to connect to Amazon CloudFront.</param> public string CreateInvalidation(string distributionId, IList<string> items, string reference, CloudFrontSettings settings) { //Get Reference if (String.IsNullOrEmpty(reference)) { reference = DateTime.Now.Ticks.ToString(); } //Correct Paths List<string> paths = new List<string>(); foreach (string item in items) { if (!item.StartsWith("/")) { paths.Add("/" + item); } } //Create Request InvalidationBatch batch = new InvalidationBatch() { Paths = new Paths() { Items = paths.ToList(), Quantity = items.Count }, CallerReference = reference }; CreateInvalidationRequest request = new CreateInvalidationRequest() { DistributionId = distributionId, InvalidationBatch = batch }; //Send Request _Log.Verbose("Create Invalidation {0}", distributionId); AmazonCloudFrontClient client = this.GetClient(settings); CreateInvalidationResponse response = client.CreateInvalidation(request); if (response.HttpStatusCode == System.Net.HttpStatusCode.Created) { return response.Invalidation.Id; } else { _Log.Error("Error invalidating object {0}", distributionId); return ""; } }
public object Execute(ExecutorContext context) { var cmdletContext = context as CmdletContext; // create request var request = new Amazon.CloudFront.Model.CreateInvalidationRequest(); if (cmdletContext.DistributionId != null) { request.DistributionId = cmdletContext.DistributionId; } // populate InvalidationBatch var requestInvalidationBatchIsNull = true; request.InvalidationBatch = new Amazon.CloudFront.Model.InvalidationBatch(); System.String requestInvalidationBatch_invalidationBatch_CallerReference = null; if (cmdletContext.InvalidationBatch_CallerReference != null) { requestInvalidationBatch_invalidationBatch_CallerReference = cmdletContext.InvalidationBatch_CallerReference; } if (requestInvalidationBatch_invalidationBatch_CallerReference != null) { request.InvalidationBatch.CallerReference = requestInvalidationBatch_invalidationBatch_CallerReference; requestInvalidationBatchIsNull = false; } Amazon.CloudFront.Model.Paths requestInvalidationBatch_invalidationBatch_Paths = null; // populate Paths var requestInvalidationBatch_invalidationBatch_PathsIsNull = true; requestInvalidationBatch_invalidationBatch_Paths = new Amazon.CloudFront.Model.Paths(); List <System.String> requestInvalidationBatch_invalidationBatch_Paths_paths_Item = null; if (cmdletContext.Paths_Item != null) { requestInvalidationBatch_invalidationBatch_Paths_paths_Item = cmdletContext.Paths_Item; } if (requestInvalidationBatch_invalidationBatch_Paths_paths_Item != null) { requestInvalidationBatch_invalidationBatch_Paths.Items = requestInvalidationBatch_invalidationBatch_Paths_paths_Item; requestInvalidationBatch_invalidationBatch_PathsIsNull = false; } System.Int32?requestInvalidationBatch_invalidationBatch_Paths_paths_Quantity = null; if (cmdletContext.Paths_Quantity != null) { requestInvalidationBatch_invalidationBatch_Paths_paths_Quantity = cmdletContext.Paths_Quantity.Value; } if (requestInvalidationBatch_invalidationBatch_Paths_paths_Quantity != null) { requestInvalidationBatch_invalidationBatch_Paths.Quantity = requestInvalidationBatch_invalidationBatch_Paths_paths_Quantity.Value; requestInvalidationBatch_invalidationBatch_PathsIsNull = false; } // determine if requestInvalidationBatch_invalidationBatch_Paths should be set to null if (requestInvalidationBatch_invalidationBatch_PathsIsNull) { requestInvalidationBatch_invalidationBatch_Paths = null; } if (requestInvalidationBatch_invalidationBatch_Paths != null) { request.InvalidationBatch.Paths = requestInvalidationBatch_invalidationBatch_Paths; requestInvalidationBatchIsNull = false; } // determine if request.InvalidationBatch should be set to null if (requestInvalidationBatchIsNull) { request.InvalidationBatch = null; } CmdletOutput output; // issue call var client = Client ?? CreateClient(_CurrentCredentials, _RegionEndpoint); try { var response = CallAWSServiceOperation(client, request); object pipelineOutput = null; pipelineOutput = cmdletContext.Select(response, this); output = new CmdletOutput { PipelineOutput = pipelineOutput, ServiceResponse = response }; } catch (Exception e) { output = new CmdletOutput { ErrorResponse = e }; } return(output); }
internal CreateInvalidationResponse CreateInvalidation(CreateInvalidationRequest request) { var task = CreateInvalidationAsync(request); try { return task.Result; } catch(AggregateException e) { throw e.InnerException; } }
IAsyncResult invokeCreateInvalidation(CreateInvalidationRequest request, AsyncCallback callback, object state, bool synchronized) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.Instance; return Invoke(request, callback, state, synchronized, marshaller, unmarshaller, signer); }
private void InvalidateCloudFront(params string[] paths) { if (!_revalidateCloudFront || string.IsNullOrEmpty(_distributionId)) return; using (var cfClient = GetCloudFrontClient()) { var invalidationRequest = new CreateInvalidationRequest { DistributionId = _distributionId, InvalidationBatch = new InvalidationBatch { CallerReference = Guid.NewGuid().ToString(), Paths = new Paths { Items = paths.ToList(), Quantity = paths.Count() } } }; cfClient.CreateInvalidation(invalidationRequest); } }
/// <summary> /// Initiates the asynchronous execution of the CreateInvalidation operation. /// <seealso cref="Amazon.CloudFront.IAmazonCloudFront.CreateInvalidation"/> /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateInvalidation operation.</param> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> /// <returns>The task object representing the asynchronous operation.</returns> public async Task<CreateInvalidationResponse> CreateInvalidationAsync(CreateInvalidationRequest request, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.GetInstance(); var response = await Invoke<IRequest, CreateInvalidationRequest, CreateInvalidationResponse>(request, marshaller, unmarshaller, signer, cancellationToken) .ConfigureAwait(continueOnCapturedContext: false); return response; }
private Amazon.CloudFront.Model.CreateInvalidationResponse CallAWSServiceOperation(IAmazonCloudFront client, Amazon.CloudFront.Model.CreateInvalidationRequest request) { Utils.Common.WriteVerboseEndpointMessage(this, client.Config, "Amazon CloudFront", "CreateInvalidation"); try { #if DESKTOP return(client.CreateInvalidation(request)); #elif CORECLR return(client.CreateInvalidationAsync(request).GetAwaiter().GetResult()); #else #error "Unknown build edition" #endif } catch (AmazonServiceException exc) { var webException = exc.InnerException as System.Net.WebException; if (webException != null) { throw new Exception(Utils.Common.FormatNameResolutionFailureMessage(client.Config, webException.Message), webException); } throw; } }
/// <summary> /// Create a new invalidation. /// </summary> /// <param name="request">Container for the necessary parameters to execute the CreateInvalidation service method.</param> /// /// <returns>The response from the CreateInvalidation service method, as returned by CloudFront.</returns> /// <exception cref="Amazon.CloudFront.Model.AccessDeniedException"> /// Access denied. /// </exception> /// <exception cref="Amazon.CloudFront.Model.BatchTooLargeException"> /// /// </exception> /// <exception cref="Amazon.CloudFront.Model.InconsistentQuantitiesException"> /// The value of Quantity and the size of Items do not match. /// </exception> /// <exception cref="Amazon.CloudFront.Model.InvalidArgumentException"> /// The argument is invalid. /// </exception> /// <exception cref="Amazon.CloudFront.Model.MissingBodyException"> /// This operation requires a body. Ensure that the body is present and the Content-Type /// header is set. /// </exception> /// <exception cref="Amazon.CloudFront.Model.NoSuchDistributionException"> /// The specified distribution does not exist. /// </exception> /// <exception cref="Amazon.CloudFront.Model.TooManyInvalidationsInProgressException"> /// You have exceeded the maximum number of allowable InProgress invalidation batch requests, /// or invalidation objects. /// </exception> public CreateInvalidationResponse CreateInvalidation(CreateInvalidationRequest request) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.Instance; return Invoke<CreateInvalidationRequest,CreateInvalidationResponse>(request, marshaller, unmarshaller); }
/// <summary> /// <para> Create a new invalidation. </para> /// </summary> /// /// <param name="createInvalidationRequest">Container for the necessary parameters to execute the CreateInvalidation service method on /// AmazonCloudFront.</param> /// /// <returns>The response from the CreateInvalidation service method, as returned by AmazonCloudFront.</returns> /// /// <exception cref="T:Amazon.CloudFront.Model.TooManyInvalidationsInProgressException" /> /// <exception cref="T:Amazon.CloudFront.Model.MissingBodyException" /> /// <exception cref="T:Amazon.CloudFront.Model.NoSuchDistributionException" /> /// <exception cref="T:Amazon.CloudFront.Model.BatchTooLargeException" /> /// <exception cref="T:Amazon.CloudFront.Model.AccessDeniedException" /> /// <exception cref="T:Amazon.CloudFront.Model.InvalidArgumentException" /> /// <exception cref="T:Amazon.CloudFront.Model.InconsistentQuantitiesException" /> /// <param name="cancellationToken"> /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// </param> public Task<CreateInvalidationResponse> CreateInvalidationAsync(CreateInvalidationRequest createInvalidationRequest, CancellationToken cancellationToken = default(CancellationToken)) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.GetInstance(); return Invoke<IRequest, CreateInvalidationRequest, CreateInvalidationResponse>(createInvalidationRequest, marshaller, unmarshaller, signer, cancellationToken); }
/// <summary> /// Initiates the asynchronous execution of the CreateInvalidation operation. /// </summary> /// /// <param name="request">Container for the necessary parameters to execute the CreateInvalidation operation on AmazonCloudFrontClient.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndCreateInvalidation /// operation.</returns> public IAsyncResult BeginCreateInvalidation(CreateInvalidationRequest request, AsyncCallback callback, object state) { var marshaller = new CreateInvalidationRequestMarshaller(); var unmarshaller = CreateInvalidationResponseUnmarshaller.Instance; return BeginInvoke<CreateInvalidationRequest>(request, marshaller, unmarshaller, callback, state); }
static void Main() { var distributionId = "E24L1FD4ZU4BTF";//"EJCDLYDTUKDBE"; var client = new AmazonCloudFrontClient(); var batch = new InvalidationBatch() .WithCallerReference(Guid.NewGuid().ToString()) .WithPaths(new Paths() .WithQuantity(1) .WithItems("/account/logon")); var request = new CreateInvalidationRequest() .WithDistributionId(distributionId) .WithInvalidationBatch(batch); var response = client.CreateInvalidation(request); var invalidationId = response.CreateInvalidationResult.Invalidation.Id; if (string.IsNullOrEmpty(invalidationId)) return; while (true) { var invalidationResponse = client.GetInvalidation( new GetInvalidationRequest().WithDistributionId(distributionId).WithId(invalidationId)); var status = invalidationResponse.GetInvalidationResult.Invalidation.Status; if (status != "InProgress") break; Console.WriteLine("Waiting 10secs before checking..."); Thread.Sleep(10000); } Console.WriteLine("Invalidation complete!"); }