Skip to main content
Solved

Python SDK


Pablo Palacios
Seasoned
Forum|alt.badge.img+3

Hello:
Please share some code example in python sdk to link a file to an asset.

thanks

Best answer by Andre Alves

I did not have time to test it, but according to the Cognite documentation, you can use FileMetadataUpdate instead of updating directly as I mentioned before. Below is an example from the Cognite documentation on how to attach labels. In your case, you can replace labels with asset_ids as the parameter.

 

Attach labels to a files:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import FileMetadataUpdate
>>> client = CogniteClient()
>>> my_update = FileMetadataUpdate(id=1).labels.add(["PUMP", "VERIFIED"])
>>> res = client.files.update(my_update)

 

The documentation is available here: https://cognite-sdk-python.readthedocs-hosted.com/en/latest/files.html

View original
Did this topic help you find an answer to your question?

5 replies

Andre Alves
MVP
Forum|alt.badge.img+13
from cognite.client import CogniteClient

# Initialize the CogniteClient
client = CogniteClient(api_key="your_api_key", project="your_project")

# Define the asset ID and the file ID
asset_id = 123456789
file_id = 987654321

# Link the file to the asset
client.files.update(id=file_id, asset_ids=[asset_id])

print(f"File with ID {file_id} linked to asset with ID {asset_id}.")

This represents the metadata for a file. It does not contain the actual file itself. This is the read-only version of FileMetadata and is used when retrieving data from CDF.

I haven't had time to test it, but based on the documentation, it should work. I will test it later, but if it works for you, let me know as well.


Pablo Palacios
Seasoned
Forum|alt.badge.img+3

@Andre Alves thanks but it gives me the error:

TypeError: FilesAPI.update() got an unexpected keyword argument 'id'

and add the library:

from cognite.client.data_classes import FileMetadataUpdate

 


Andre Alves
MVP
Forum|alt.badge.img+13
  • MVP
  • 147 replies
  • Answer
  • July 3, 2024

I did not have time to test it, but according to the Cognite documentation, you can use FileMetadataUpdate instead of updating directly as I mentioned before. Below is an example from the Cognite documentation on how to attach labels. In your case, you can replace labels with asset_ids as the parameter.

 

Attach labels to a files:

>>> from cognite.client import CogniteClient
>>> from cognite.client.data_classes import FileMetadataUpdate
>>> client = CogniteClient()
>>> my_update = FileMetadataUpdate(id=1).labels.add(["PUMP", "VERIFIED"])
>>> res = client.files.update(my_update)

 

The documentation is available here: https://cognite-sdk-python.readthedocs-hosted.com/en/latest/files.html


Pablo Palacios
Seasoned
Forum|alt.badge.img+3

@Andre Alves 

Your last code worked well for me with the recommendations you give, thank you very much


Andre Alves
MVP
Forum|alt.badge.img+13

Good to know! I hope you are doing well with your challenges. See you soon!


Reply


Cookie Policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie Settings