sams.assets
– Internal Service for Assets¶
Asset service, used for CRUD operations on Asset metadata and associated binary data.
Usage¶
The Asset service instance can be found under sams.assets.get_service
and used:
from sams.assets import get_service
def test_assets():
assets_service = get_service()
assets_service.post([{...}])
assets_service.patch(asset_id, {...})
assets_service.delete_action({...})
assets_service.find({...})
This service instance can only be used after the application has bootstrapped
Example usage of uploading Asset Metadata and Binary:
from bson import ObjectId
from sams.assets import get_service
def test_upload_file(set_id: ObjectId):
asset_service = get_service()
with open('tests/fixtures/file_example-jpg.jpg', 'rb') as f:
asset_service.post([{
'set_id': set_id,
'filename': 'file_example-jpg.jpg',
'name': 'Jpeg Example',
'description': 'Jpeg file asset example',
'binary': f
}])
# With the above, the system will automatically set:
{
'_media_id': '5f16394239d0077e02b09d85',
'length': 12186,
'mimetype': 'image/jpeg'
}
Resource Schema¶
Service¶
-
class
AssetsService
(datasource: Optional[str] = None, backend=None)¶ -
post
(docs: List[Dict[str, Any]], **kwargs) → List[bson.objectid.ObjectId]¶ Uploads binary and stores metadata
- Parameters
docs – An array of metadata & binaries to create
kwargs – Dictionary containing the keyword arguments
- Returns
list of generated IDs for the new documents
- Return type
-
patch
(item_id: bson.objectid.ObjectId, updates: Dict[str, Any]) → Dict[str, Any]¶ Updates the binary and/or metadata
Note
When uploading a new binary to an existing Asset, the original binary will be deleted from the StorageDestination.
- Parameters
item_id (bson.objectid.ObjectId) – ID for the Asset
updates (dict) – Dictionary containing the desired metadata/binary to update
- Returns
Dictionary containing the updated attributes of the Asset
- Return type
-
on_deleted
(doc: sams_client.schemas.assets.IAsset)¶ Delete the Asset Binary after the Metadata is deleted
- Parameters
doc (dict) – The Asset that was deleted
-
upload_binary
(asset: Dict[str, Any], content: Union[BinaryIO, bytes], delete_original: bool = True) → dict¶ Uploads binary data for provided Asset
- Parameters
asset (dict) – The Asset Metadata used to store the binary for
content (io.BytesIO) – The Asset Binary to upload
delete_original (bool) – If
True
, deletes the existing binary (if any)
- Returns
Returns the
_media_id
,length
andmimetype
attributes of the binary- Return type
-
download_binary
(asset_id: Union[bson.objectid.ObjectId, str]) → superdesk.storage.superdesk_file.SuperdeskFile¶ Downloads the Asset Binary
- Parameters
asset_id (bson.objectid.ObjectId) – The ID of the Asset
- Returns
The Binary Stream for the Asset Binary
- Return type
superdesk.storage.superdesk_file.SuperdeskFile
-