Automatic encoding implementation using Cloud Functions
    • PDF

    Automatic encoding implementation using Cloud Functions

    • PDF

    Article Summary

    Available in Classic and VPC

    VOD Station encodes using the connection with Object Storage bucket, however, it does not provide the feature to automatically encode by checking if the file was uploaded to the bucket. However, you can implement a similar function utilizing the event management feature and Cloud Functions.

    Automatic encoding flow

    The order of implementing automatic encoding is as follows.

    1. Upload media files to Object Storage
    2. Trigger uploaded event from Object Storage
    3. Request encoding API for VOD Station through Cloud Functions
    4. Perform encoding at VOD Station

    Implementation Guide

    An example of implementing automatic encoding using Object Storage, Cloud Functions, and VOD Station API is as follows.

    1. Set an event to the bucket to store the original file of Object Storage
      1. Click the Event management menu of the bucket you wish to set an event at Object Storage > Bucket Management
      2. Event management pop-up > click the create button
      3. Create a bucket event
        • Enter event name
        • Filter settings: enter the condition to be used when filtering events for a specific object
          • Enter the condition as a regular expression. (<example> prefix images/: ^images/)
          • The filter applies to the object path. Events occur for all objects if no filter is entered.
        • Event type: unit creation (PUT)
          • Upon a new file upload, both POST and PUT events occur at the same time. Therefore, if you select both POST and PUT, the encoding action trigger may occur twice.
          • Overwriting can only happen for PUT events.
        • Target: cloud Functions
          • If you do not have a trigger created before, click the Create trigger button to create a trigger.
            • In the Create Trigger pop-up, enter only the name and click the create button.
            • You must set the action to connect to the trigger (logic to perform), however, if you haven't made one already, skip it.
          • Select the created trigger
            • You do not require a connected action at this stage.
          • Read the recursive call caution and mark it checked.
    2. Action setting in Cloud Functions
      1. Click the Cloud Functions > Action > Create action button
      2. Set the trigger condition on the Create Action page
        • Trigger type: select Object Storage.
        • Name: select the trigger you created in the previous stage and click the add button.
        • Read the recursive call caution and mark it checked.
      3. Set the logic for the Action
        • Package: select a package (group) you want. If there is none, create a new one.
        • Type: basic (can select sequence if needed)
        • Name: enter a name for the action you’re creating.
        • Source code: the customer must implement the business logic for the creating action themselves. Please see the example source code at the bottom of this guide.
        • VPC connection information: if the selected platform is a VPC environment, select a VPC and subnet to connect to. If not, create a new one.

    Automatic encoding action source code setting

    An example Cloud Functions settings is as follows.

    Note

    Since the example code does not guarantee normal operation, use it as a reference for actual implementation.

    • Example parameter
      • Default Parameter (JSON Object)

        • Set as below.
        {
            "base_url": "https://vodstation.apigw.ntruss.com",
            "api_url": "/api/v2/category/{CATEGORY-ID}/add-files",
            "access_key": "{API-ACCESS-KEY}",
            "secret_key": "{API-SECRET-KEY}"
        }
        
        • {CATEGORY-ID}: substitute with the category ID for the encoding.
        • {API-ACCESS-KEY}: substitute with the user's Access Key.
        • {API-SECRET-KEY}: substitute with the user's Secret Key.
    Note

    CATEGORY-ID can be checked through the category list search API, and API-ACCESS-KEY and API-SECRET-KEY can be checked in authentication key management.

    • Example source code

      • The example was implemented on Python 3.7 and it can be implemented in a different language.
      import hashlib
      import hmac
      import base64
      import requests
      import time
      import json
      
      
      def make_signature(url, timestamp, access_key, secret_key):
          timestamp = int(time.time() * 1000)
          timestamp = str(timestamp)
      
          secret_key = bytes(secret_key, "UTF-8")
      
          method = "PUT"
      
          message = method + " " + url + "\n" + timestamp + "\n" + access_key
          message = bytes(message, "UTF-8")
          sign_key = base64.b64encode(
              hmac.new(secret_key, message, digestmod=hashlib.sha256).digest()
          )
          print(sign_key)
          return sign_key
      
      
      def make_header(timestamp, access_key, sign_key):
          headers = {
              "Content-Type": "application/json; charset=utf-8",
              "x-ncp-apigw-timestamp": timestamp,
              "x-ncp-iam-access-key": access_key,
              "x-ncp-apigw-signature-v2": sign_key,
          }
          return headers
      
      
      def main(args):
          object_name = args.get("object_name")
          input_bucket_name = args.get("container_name")
      
          api_url = args["api_url"]
          full_url = f'{args["base_url"]}{api_url}'
          timestamp = str(int(time.time() * 1000))
      
          sign_key = make_signature(
              api_url, timestamp, args["access_key"], args["secret_key"]
          )
          headers = make_header(timestamp, args["access_key"], sign_key)
      
          try:
              res = requests.put(full_url, headers=headers, data=json.dumps({'bucketName' : input_bucket_name, 'pathList': [ object_name ] }))
      
              if res.status_code == 200:
                  return {"input_bucket": input_bucket_name, "object_path": object_name, "res": res.text, "done": True}
              else:
                  raise Exception({"done": False, "error_message": res.text})
      
          except Exception as e:
              raise Exception({"done": False, "error_message": str(e)})
      

    Was this article helpful?

    Changing your password will log you out immediately. Use the new password to log back in.
    First name must have atleast 2 characters. Numbers and special characters are not allowed.
    Last name must have atleast 1 characters. Numbers and special characters are not allowed.
    Enter a valid email
    Enter a valid password
    Your profile has been successfully updated.