Skip to content
This repository was archived by the owner on Dec 5, 2025. It is now read-only.

Commit ae00059

Browse files
authored
Merge pull request #632 from OpenCTI-Platform/issue/631
[client-python] upload_file now accepts optional arg file_markings (#631)
2 parents 38110e8 + 9d6d29a commit ae00059

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

examples/upload_file.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# coding: utf-8
22

3+
from stix2 import TLP_GREEN
4+
35
from pycti import OpenCTIApiClient
46

57
# Variables
@@ -9,8 +11,11 @@
911
# OpenCTI initialization
1012
opencti_api_client = OpenCTIApiClient(api_url, api_token)
1113

12-
# Upload the file
14+
# Get a marking
15+
TLP_GREEN_CTI = opencti_api_client.marking_definition.read(id=TLP_GREEN["id"])
16+
17+
# Upload the file (note that markings are optional)
1318
file = opencti_api_client.upload_file(
14-
file_name="./upload_file_example.pdf",
19+
file_name="./upload_file_example.pdf", file_markings=[TLP_GREEN["id"]]
1520
)
1621
print(file)

pycti/api/opencti_api_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -616,18 +616,19 @@ def upload_file(self, **kwargs):
616616
"""upload a file to OpenCTI API
617617
618618
:param `**kwargs`: arguments for file upload (required: `file_name` and `data`)
619-
:return: returns the query respons for the file upload
619+
:return: returns the query response for the file upload
620620
:rtype: dict
621621
"""
622622

623623
file_name = kwargs.get("file_name", None)
624+
file_markings = kwargs.get("file_markings", None)
624625
data = kwargs.get("data", None)
625626
mime_type = kwargs.get("mime_type", "text/plain")
626627
if file_name is not None:
627628
self.app_logger.info("Uploading a file.")
628629
query = """
629-
mutation UploadImport($file: Upload!) {
630-
uploadImport(file: $file) {
630+
mutation UploadImport($file: Upload!, $fileMarkings: [String]) {
631+
uploadImport(file: $file, fileMarkings: $fileMarkings) {
631632
id
632633
name
633634
}
@@ -639,8 +640,11 @@ def upload_file(self, **kwargs):
639640
mime_type = "application/json"
640641
else:
641642
mime_type = magic.from_file(file_name, mime=True)
642-
643-
return self.query(query, {"file": (File(file_name, data, mime_type))})
643+
query_vars = {"file": (File(file_name, data, mime_type))}
644+
# optional file markings
645+
if file_markings is not None:
646+
query_vars["fileMarkings"] = file_markings
647+
return self.query(query, query_vars)
644648
else:
645649
self.app_logger.error("[upload] Missing parameter: file_name")
646650
return None

0 commit comments

Comments
 (0)