Skip to main content

How to send a file in a Trigger event

Sometimes you may want to send send a file in a Trigger event. There are two ways you can accomplish this:

  1. Send the file as a link
  2. Send the file as a base64 string in the request body

The preferred and most simple way to send a file with the Trigger API is to first upload the file to a storage location and add the authenticated or public link to the request body of the Trigger event. For example, before constructing the Trigger event, upload the file you wish to send to a storage location (i.e. Google Cloud Storage, Amazon S3, Dropbox, etc.) and generate a link that is accessible via http. Then, in the request body for the Trigger event, include a property like "{"linked_file": "url_to_file_here"}". Once Kimiko processes the Trigger event, you can access the linked file in subsequent workflows and actions.

Pros:

  • Simple and easy way to send a file
  • You have full control over the lifecycle of file

Cons:

  • Beware of storing sensitive data in public links
  • Authenticated links expire

Example using Google Cloud Storage

1. Step One: upload the file to Google Cloud Storage

There are many ways to do this. Google offers client libraries as well command line tools to upload files to storage. See their documentation for more details or check out the example Python code below:

from google.cloud import storage


def upload_blob_from_memory(bucket_name, contents, destination_blob_name):
"""Uploads a file to the bucket."""

# The ID of your GCS bucket
# bucket_name = "your-bucket-name"

# The contents to upload to the file
# contents = "these are my contents"

# The ID of your GCS object
# destination_blob_name = "storage-object-name"

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(destination_blob_name)

blob.upload_from_string(contents)

print(
f"{destination_blob_name} with contents {contents} uploaded to {bucket_name}."
)

If the file you upload to Google is a public file or exists in a public folder, you can simply use the public link. However, many files in Cloud Storage are not public so instaed you must generate a signed-url (pre-authenticated link). See Google Cloud's docuemnation for how to gernate a signed-url or check out the Python code below:

def generate_download_signed_url_v4(bucket_name, blob_name):
"""Generates a v4 signed URL for downloading a blob.

Note that this method requires a service account key file. You can not use
this if you are using Application Default Credentials from Google Compute
Engine or from the Google Cloud SDK.
"""
# bucket_name = 'your-bucket-name'
# blob_name = 'your-object-name'

storage_client = storage.Client()
bucket = storage_client.bucket(bucket_name)
blob = bucket.blob(blob_name)

url = blob.generate_signed_url(
version="v4",
# This URL is valid for 15 minutes
expiration=datetime.timedelta(minutes=15),
# Allow GET requests using this URL.
method="GET",
)

3. Step Three: use Kimiko's Trigger API to send the url

Use the Trigger API and include the url in the request body.

{
"linked_file": "<url generaged in step two>"
}

4. Step Four: access the linked file in a Kimiko Action

Now that the linked file is a part of the Trigger event, you can use the linked file in downstream acations. For example, in an HTTPS action, you could access the link file using {{request.body.linked_file}}.

2. Send the file as a base64 string in the request body

The other alternative is to convert the file to base64 before sending. The downside to this approach is file size as Kimiko Trigger events are limited to 1MB.

Eample using Python

Step One: conver the file to base64

def file_to_base64(filename):
"""
Converts a file to base64 in memory.

Args:
filename: The path to the file to be converted.

Returns:
The base64 encoded string of the file.
"""

with open(filename, "rb") as f:
data = f.read()

return base64.b64encode(data).decode()

Step Two: use Kimiko's Trigger API and add the base64 file string to the request body

Now that the file can be represented as a string, you can send it as a part of the Trigger API request.

{
"file": "<base64 string of file>"
}

Step Three: access the linked file in a Kimiko Action

Now that the file is a part of the Trigger event, you can use the file in downstream acations. For example, in an HTTPS action, you could access the link file using {{request.body.file}}. The file can then be decoded on whichever services is processing the action.