Pixta Cloud

Document
Home/Asynchronous Image Processing/

Asynchronous Image Processing

Unlike main flow - process image on-demand, this will process image in background - asynchronously

Pre-condition

  • - An API key
  • - A webhook endpoint URL
  • - Config your destination storage - where processed images will be stored

Send request to processing image

import requests

url = "https://api.pvnservices.com/api/processes/v1/background_processes"
headers = {
  "x-api-key": "your-api-key"
}
payload = {
  "source_id": "62ec7b0041e66d000146c60a",
  "object_key": "000/018/083/18083.jpg",
  "parameters": {
    "w": 1600,
    "h": 1600
  },
  "destination_object_key": "000/018/083/18083.jpg"
}

response = requests.post(url, headers = headers, data = payload)

Pixta Cloud will enqueue the request processing image, this request will be processed and put the result(processed image) to the destination storage which configured.

After the processed image is put to destination storage, Pixta Cloud will notify to you by webhook endpoint URL use POST method. The payload following by example:

{
  "status_code": 200,
  "message": "Success",
  "response": {
    "source_id": "62ec7b0041e66d000146c60a",
    "job_id": "2cade2860ffd188f2c4bd89e",
    "object_puts": [
      {
        "bucket": "example-bucket-name",
        "key": "000/018/083/18083.jpg"
      },
      {
        "bucket": "example-2-bucket-name",
        "key": "000/018/083/18083.jpg"
      }
    ],
    "mime": "image/jpeg",
    "size": "170000"
  }
}

Securing your webhooks

When your server is configured to receive payloads, it will listen for any payload sent to the endpoint you configured. You probably want to limit requests to those coming from Pixta Cloud.

You can get secret token from Webhooks page in Console, then config endpoint to restrict payloads which is not be signed. For example:


  def hooked
    head 400 unless verify_signature
    data = JSON.parse(params)
    puts "I got some JSON: #{data.inspect}"
  end

  def verify_signature
    signature = 'sha256=' + OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), ENV['WEBHOOK_ENDPOINT_SECRET_TOKEN'], params)
    Rack::Utils.secure_compare(signature, headers['x-ims-signature-256'])
  end

Your programing language from this example code. However, there are important things notice:

  • The hash signature starts with sha256=, using the key of your secret token and request payload body.
  • We recommend use method like secure_compare performs a "constant time" string comparison, which helps palliate certain timing attacks against regular equality operators.