Upload a string or bytes

async AsyncCogniteClient.files.upload_bytes(
content: str | bytes | BinaryIO | AsyncIterator[bytes],
name: str,
external_id: str | None = None,
source: str | None = None,
mime_type: str | None = None,
metadata: dict[str, str] | None = None,
directory: str | None = None,
asset_ids: Sequence[int] | None = None,
data_set_id: int | None = None,
labels: Sequence[Label] | None = None,
geo_location: GeoLocation | None = None,
source_created_time: int | None = None,
source_modified_time: int | None = None,
security_categories: Sequence[int] | None = None,
overwrite: bool = False,
) FileMetadata

Upload bytes or string.

You can also pass a file handle to ‘content’. The file must be opened in binary mode or an error will be raised.

Note that the maximum file size is 5GiB. In order to upload larger files use multipart_upload_session.

Parameters:
  • content (str | bytes | BinaryIO | AsyncIterator[bytes]) – The content to upload.

  • name (str) – Name of the file.

  • external_id (str | None) – The external ID provided by the client. Must be unique within the project.

  • source (str | None) – The source of the file.

  • mime_type (str | None) – File type. E.g. text/plain, application/pdf,…

  • metadata (dict[str, str] | None) – Customizable extra data about the file. String key -> String value.

  • directory (str | None) – The directory to be associated with this file. Must be an absolute, unix-style path.

  • asset_ids (Sequence[int] | None) – No description.

  • data_set_id (int | None) – Id of the data set.

  • labels (Sequence[Label] | None) – A list of the labels associated with this resource item.

  • geo_location (GeoLocation | None) – The geographic metadata of the file.

  • source_created_time (int | None) – The timestamp for when the file was originally created in the source system.

  • source_modified_time (int | None) – The timestamp for when the file was last modified in the source system.

  • security_categories (Sequence[int] | None) – Security categories to attach to this file.

  • overwrite (bool) – If ‘overwrite’ is set to true, and the POST body content specifies a ‘externalId’ field, fields for the file found for externalId can be overwritten. The default setting is false. If metadata is included in the request body, all of the original metadata will be overwritten. The actual file will be overwritten after successful upload. If there is no successful upload, the current file contents will be kept. File-Asset mappings only change if explicitly stated in the assetIds field of the POST json body. Do not set assetIds in request body if you want to keep the current file-asset mappings.

Returns:

The metadata of the uploaded file.

Return type:

FileMetadata

Examples

Upload a file from memory:

>>> from cognite.client import CogniteClient, AsyncCogniteClient
>>> client = CogniteClient()
>>> # async_client = AsyncCogniteClient()  # another option
>>> res = client.files.upload_bytes(b"some content", name="my_file", asset_ids=[1, 2, 3])