Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set a default size when using SpooledTemporaryFile #1359

Merged
merged 1 commit into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/backends/amazon-S3.rst
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ Settings

``max_memory_size`` or ``AWS_S3_MAX_MEMORY_SIZE``

Default: ``0`` i.e do not roll over
Default: ``FILE_UPLOAD_MAX_MEMORY_SIZE``

The maximum amount of memory (in bytes) a file can take up before being rolled over
into a temporary file on disk.
Expand Down
4 changes: 2 additions & 2 deletions docs/backends/azure.rst
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ Settings

Global connection timeout in seconds.

``max_memory`` size ``AZURE_BLOB_MAX_MEMORY_SIZE``
``max_memory_size`` size ``AZURE_BLOB_MAX_MEMORY_SIZE``

Default: ``2*1024*1024`` i.e ``2MB``
Default: ``FILE_UPLOAD_MAX_MEMORY_SIZE``

Maximum memory used by a downloaded file before dumping it to disk in bytes.

Expand Down
5 changes: 5 additions & 0 deletions docs/backends/dropbox.rst
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ Settings

Sets the Dropbox WriteMode strategy. Read more in the `official docs`_.

``max_memory_size`` size ``DROPBOX_MAX_MEMORY_SIZE``

Default: ``FILE_UPLOAD_MAX_MEMORY_SIZE``

Maximum memory used by a downloaded file before dumping it to disk in bytes.

.. _`tutorial`: https://www.dropbox.com/developers/documentation/python#tutorial
.. _`Dropbox SDK for Python`: https://www.dropbox.com/developers/documentation/python#tutorial
Expand Down
4 changes: 2 additions & 2 deletions docs/backends/gcloud.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,10 @@ Settings

``max_memory_size`` or ``GS_MAX_MEMORY_SIZE``

default: ``0`` i.e do not rollover
default: ``FILE_UPLOAD_MAX_MEMORY_SIZE``

The maximum amount of memory a returned file can take up (in bytes) before being
rolled over into a temporary file on disk. Default is 0: Do not roll over.
rolled over into a temporary file on disk.

``blob_chunk_size`` or ``GS_BLOB_CHUNK_SIZE``

Expand Down
6 changes: 4 additions & 2 deletions storages/backends/azure_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def _get_file(self):
file = SpooledTemporaryFile(
max_size=self._storage.max_memory_size,
suffix=".AzureStorageFile",
dir=setting("FILE_UPLOAD_TEMP_DIR", None),
dir=setting("FILE_UPLOAD_TEMP_DIR"),
)

if "r" in self._mode or "a" in self._mode:
Expand Down Expand Up @@ -141,7 +141,9 @@ def get_default_settings(self):
"azure_ssl": setting("AZURE_SSL", True),
"upload_max_conn": setting("AZURE_UPLOAD_MAX_CONN", 2),
"timeout": setting("AZURE_CONNECTION_TIMEOUT_SECS", 20),
"max_memory_size": setting("AZURE_BLOB_MAX_MEMORY_SIZE", 2 * 1024 * 1024),
"max_memory_size": setting(
"AZURE_BLOB_MAX_MEMORY_SIZE", setting("FILE_UPLOAD_MAX_MEMORY_SIZE")
),
"expiration_secs": setting("AZURE_URL_EXPIRATION_SECS"),
"overwrite_files": setting("AZURE_OVERWRITE_FILES", False),
"location": setting("AZURE_LOCATION", ""),
Expand Down
9 changes: 8 additions & 1 deletion storages/backends/dropbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ def __init__(self, name, storage):

def _get_file(self):
if self._file is None:
self._file = SpooledTemporaryFile()
self._file = SpooledTemporaryFile(
max_size=self._storage.max_memory_size,
suffix=".DropboxFile",
dir=setting("FILE_UPLOAD_TEMP_DIR"),
)
# As dropbox==9.3.0, the client returns a tuple
# (dropbox.files.FileMetadata, requests.models.Response)
file_metadata, response = self._storage.client.files_download(self.name)
Expand Down Expand Up @@ -119,6 +123,9 @@ def get_default_settings(self):
"oauth2_refresh_token": setting("DROPBOX_OAUTH2_REFRESH_TOKEN"),
"timeout": setting("DROPBOX_TIMEOUT", _DEFAULT_TIMEOUT),
"write_mode": setting("DROPBOX_WRITE_MODE", _DEFAULT_MODE),
"max_memory_size": setting(
"DROPBOX_MAX_MEMORY_SIZE", setting("FILE_UPLOAD_MAX_MEMORY_SIZE")
),
}

def _full_path(self, name):
Expand Down
7 changes: 4 additions & 3 deletions storages/backends/gcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ def get_default_settings(self):
"file_overwrite": setting("GS_FILE_OVERWRITE", True),
"object_parameters": setting("GS_OBJECT_PARAMETERS", {}),
# The max amount of memory a returned file can take up before being
# rolled over into a temporary file on disk. Default is 0: Do not
# roll over.
"max_memory_size": setting("GS_MAX_MEMORY_SIZE", 0),
# rolled over into a temporary file on disk.
"max_memory_size": setting(
"GS_MAX_MEMORY_SIZE", setting("FILE_UPLOAD_MAX_MEMORY_SIZE")
),
"blob_chunk_size": setting("GS_BLOB_CHUNK_SIZE"),
}

Expand Down
4 changes: 3 additions & 1 deletion storages/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,9 @@ def get_default_settings(self):
"region_name": setting("AWS_S3_REGION_NAME"),
"use_ssl": setting("AWS_S3_USE_SSL", True),
"verify": setting("AWS_S3_VERIFY", None),
"max_memory_size": setting("AWS_S3_MAX_MEMORY_SIZE", 0),
"max_memory_size": setting(
"AWS_S3_MAX_MEMORY_SIZE", setting("FILE_UPLOAD_MAX_MEMORY_SIZE")
),
"default_acl": setting("AWS_DEFAULT_ACL", None),
"use_threads": setting("AWS_S3_USE_THREADS", True),
"transfer_config": setting("AWS_S3_TRANSFER_CONFIG", None),
Expand Down
Loading