Attachments
Learn more about how Sentry can store additional files in the same request as event attachments.
Sentry can enrich your events for further investigation by storing additional files, such as config or log files, as attachments.
Because attachments live on the Scope, the SDK sends all attachments on a given Scope with every non-transaction event captured within the Scope. It's also possible to send an attachment with all events, including transactions by setting the add_to_transactions option to True.
# Add an attachment
import sentry_sdk
scope = sentry_sdk.get_current_scope()
scope.add_attachment(bytes=b"Hello World!", filename="attachment.txt")
scope.add_attachment(path="/path/to/attachment/file.txt")
The add_attachment method has the following parameters:
bytes: Raw bytes of the attachment, or a function that returns the raw bytes. Must be provided unless apathto the attachment file is provided.filename: The filename of the attachment which we display in Sentry. Optional only if apathis provided, since we can infer thefilenamefrom thepath.path: Path to a file to attach. Must be provided unlessbytesis provided.content_type: The attachment's MIME type. If not provided, it will be guessed from the attachment's file name provided viafilenameor inferred frompath.add_to_transactions: Whether to add this attachment to transactions. Defaults toFalse.
Sentry allows at most 20MB for a compressed request, and at most 100MB of uncompressed attachments per event, including the crash report file (if applicable). Uploads exceeding this size are rejected with HTTP error 413 Payload Too Large and the data is dropped immediately. To add larger or more files, consider secondary storage options.
Attachments persist for 30 days; if your total storage included in your quota is exceeded, attachments will not be stored. You can delete attachments or their containing events at any time. Deleting an attachment does not affect your quota - Sentry counts an attachment toward your quota as soon as it is stored.
Learn more about how attachments impact your quota.
It's possible to add, remove, or modify attachments before an event is sent by way of the before_sendhook or an event processor.
from sentry_sdk.scope import add_global_event_processor
from sentry_sdk.types import Event, Hint
from sentry_sdk.attachments import Attachment
@add_global_event_processor
def event_processor(event: Event, hint: Hint):
hint["attachments"] = [Attachment(bytes=b"Hello World!", filename="attachment.txt")]
return event
To limit access to attachments, navigate to your organization's General Settings, then select the Attachments Access dropdown to set appropriate access — any member of your organization, the organization billing owner, member, admin, manager, or owner.