File upload

File integration upload files

There are currently two places on MaiAgent where files need to be uploaded

  1. Knowledge document upload

  2. Attachment upload for messages

Presigned upload mode

MaiAgent supports Presigned upload mode, this mode allows the client to upload files directly to cloud storage (such as S3) without passing through the server as an intermediary. When using this mode, the server is only responsible for generating time-limited and secure Presigned URLs, and the client uses that URL to upload the file directly.

Differences from the traditional upload mode:

  1. Data flow

    • Traditional mode: files must go through the server, and then the server uploads them to cloud storage.

    • Presigned mode: files are uploaded directly from the client to the cloud, avoiding the server's burden of handling files.

  2. Performance and cost

    • The traditional mode can cause excessive server resource consumption and increase data transfer costs.

    • Presigned mode reduces server load, improves performance, and lowers transfer costs.

  3. Security

    • Presigned URLs include a signature and expiration, ensuring upload requests can only be used by authorized users within the specified time.

This mode is especially suitable for scenarios with large files or high-frequency uploads, improving efficiency while maintaining high security.

Presigned upload flowchart

Traditional upload flowchart


Upload file with Presigned URL

The following describes how to use the API provided by MaiAgent to complete the file Presigned upload process.

1. Obtain Presigned URL

Endpoint

POST https://api.maiagent.ai/api/v1/upload-presigned-url/

Description

The client sends a request to the server to obtain a Presigned URL, which is used to upload files directly to cloud storage.

Request parameters

Parameter name
Type
Required
Description

filename

string

Yes

Name of the file to be uploaded

modelName

string

Yes

Module name, used to categorize the purpose of the uploaded file

For knowledge base please fill inchatbot-file

For message attachments please fill inattachment

fieldName

string

Yes

File field name, used to identify the file purpose

fileSize

integer

Yes

File size (in bytes)

Example request

curl --location 'https://api.maiagent.ai/api/v1/upload-presigned-url/' \
--header 'Authorization: Api-Key ' \
--header 'Content-Type: application/json' \
--data '{
    "filename": "Products_example.xlsx",
    "modelName": "chatbot-file",
    "fieldName": "file",
    "fileSize": 5881
}'

Example response

{
    "url": "https://s3.ap-northeast-1.amazonaws.com/whizchat-media-prod-django.playma.app",
    "fields": {
        "key": "media/chatbots/chatbot-file/d83c88aa-3874-49a6-b01e-1948ae48b747.xlsx",
        "x-amz-algorithm": "AWS4-HMAC-SHA256",
        "x-amz-credential": "AKIATIVCN4X5JXWAP43M/20241129/ap-northeast-1/s3/aws4_request",
        "x-amz-date": "20241129T012318Z",
        "policy": "eyJleHBpcmF0aW9uIjogIjIwMjQtMTEtMjlUMDI6MjM6MThaIiwgImNvbmRpdGlvbnMiOiBbWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzYwMF0sIHsieW = \"
        "x-amz-signature": "0254b3baa65c3a69eaeeccd0a3d027bc7952c1587cc96437c096eb1b76e1d692"
    }
}

2. Upload file to cloud storage

Description

Use the url and fields, obtained from the above steps to directly upload the file to cloud storage.

Example request

curl --location 'https://s3.ap-northeast-1.amazonaws.com/whizchat-media-prod-django.playma.app' \
--form 'key="media/chatbots/chatbot-file/d83c88aa-3874-49a6-b01e-1948ae48b747.xlsx"' \
--form 'x-amz-algorithm="AWS4-HMAC-SHA256"' \
--form 'x-amz-credential="AKIATIVCN4X5JXWAP43M/20241129/ap-northeast-1/s3/aws4_request"' \
--form 'x-amz-date="20241129T012318Z"' \
--form 'policy="eyJleHBpcmF0aW9uIjogIjIwMjQtMTEtMjlUMDI6MjM6MThaIiwgImNvbmRpdGlvbnMiOiBbWyJjb250ZW50LWxlbmd0aC1yYW5nZSIsIDAsIDEwNDg1NzYwMF0sIHsieW = \"' \
--form 'x-amz-signature="0254b3baa65c3a69eaeeccd0a3d027bc7952c1587cc96437c096eb1b76e1d692"' \
--form 'file=@"/Users/maiagent/Downloads/Products_example.png"'

Last updated

Was this helpful?