Attachments: Copy, Move, Rename

Attachments can be copied, moved, or renamed using built-in session methods.
All of those actions are executed when save_changes is called and take place on the server-side,
removing the need to transfer the entire attachment binary data over the network in order to perform the action.

Copy attachment

Attachment can be copied using one of the session.advanced.attachments.copy methods:

Syntax

def copy(
    self,
    entity_or_document_id: Union[object, str],
    source_name: str,
    destination_entity_or_document_id: object,
    destination_name: str,
) -> None: ...

Example

employee_1 = session.load("employees/1-A")
employee_2 = session.load("employees/2-A")

session.advanced.attachments.copy(employee_1, "photo.jpg", employee_2, "photo-copy.jpg")

session.save_changes()

Move attachment

Attachment can be moved using one of the session.advanced.attachments.move methods:

Syntax

def move(
    self,
    source_entity_or_document_id: Union[str, object],
    source_name: str,
    destination_entity_or_document_id: Union[str, object],
    destination_name: str,
) -> None: ...

Example

employee1 = session.load("employees/1-A")
employee2 = session.load("employees/2-A")

session.advanced.attachments.move(employee1, "photo.jpg", employee2, "photo.jpg")

session.save_changes()

Rename attachment

Attachment can be renamed using one of the session.advanced.attachments.rename methods:

Syntax

def rename(self, entity_or_document_id: Union[str, object], name: str, new_name: str) -> None: ...

Example

employee = session.load("employees/1-A", Employee)

session.advanced.attachments.rename(employee, "photo.jpg", "photo-new.jpg")

session.save_changes()