Test Enterprise-Class Web CMS Scrivito Free for 30 Days
Test Scrivito Free for 30 Days

Upload a blob

Multistep process for uploading a binary file (JPG, PDF, etc.) for referencing it from an object attribute.

Step 1: Request an upload permission from Scrivito

The response grants the client permission to upload a file to Scrivito’s blob storage.

GET /tenants/:tenant_id/blobs/upload_permission

Params

  • tenant_id – The ID of the tenant.

Response

The blob upload permission.

Example

curl \
  -X GET \
  -H 'Content-Type: application/json' \
  -u 'api_token:MYTOKEN' \
  https://api.scrivito.com/tenants/:tenant_id/blobs/upload_permission
{
  "url": "https://xyz.../",
  "fields": {
    "foo": "bar",
    "bar": "baz",
    ...
  },
  "blob": "e13f0d960feeb2b29e9103b203ae03bd"
}

Step 2: Upload the file to the URL provided in the response

Provide the fields as parameters. The storage server itself then verifies the signature of these parameters prior to accepting the upload. Specify all the fields, but don’t hard-code this list anywhere in your code. Instead, process the fields in the fields value dynamically. Make sure to add a file field as the last field.

If successful, take the blob value of the upload permission and use it as an upload parameter in the next step.

Example

curl \
  -X POST \
  -F "foo=bar" \
  -F "bar=baz" \
  ...
  -F "file=@/path/to/the/image.jpg" \
  https://xyz.../

Step 3: Activate the uploaded blob

Until then, the blob upload is only temporary and is treated as garbage to be collected eventually. Activation instructs Scrivito to keep the blob and make it ready for referencing it from objects.

PUT /tenants/:tenant_id/blobs/activate_upload

Params

  • tenant_id – The ID of the tenant.

Params as part of the payload

  • filename – The name of the blob. This filename will become part of the blob’s download URL and hence be the suggested filename when saving the blob to a local disk.
  • content_type – The MIME-Type of the blob content.
  • obj_id – The ID of the CMS object the blob should be activated for. Only this object is allowed to reference the blob afterwards. To create a new object, specify a random ID.
  • upload – The value of the blob field from step 2.

Response

The blob handle. It includes the id of the blob.

Since this action is performed in the background, please refer to the tasks API for polling the task status until the result is returned.

Example

curl \
  -X PUT \
  -H 'Content-Type: application/json' \
  -u 'api_token:MYTOKEN' \
  -d '{
    "filename": "sleeping_cat.jpg",
    "content_type": "image/jpeg",
    "obj_id": "...",
    "upload": "e13f0d960feeb2b29e9103b203ae03bd"
  }' \
  https://api.scrivito.com/tenants/:tenant_id/blobs/activate_upload
{
  "id": "07de768d8a8eeb69/a5fb72f13fb5/sleeping_cat.jpg"
}

Step 4: Reference the blob from a CMS object

Refer to Create or update an object for how to set object attributes. The value of a binary attribute needs to be specified as the pair ["binary", {"id": blob_id}].

Example

curl \
  -X PUT \
  -H 'Content-Type: application/json' \
  -u 'api_token:MYTOKEN' \
  -d '{
    "obj": {
      "_id": "07de768d8a8eeb69",
      "_obj_class": "Image",
      "blob": ["binary", {
        "id": "07de768d8a8eeb69/a5fb72f13fb5/sleeping_cat.jpg"
      }],
      "title": ["string", "Sleeping Cat"]
    }
  }' \
  https://api.scrivito.com/tenants/:tenant_id/workspaces/:ws_id/objs/:obj_id