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

Close SSH connection of SFTP storage #1253

Merged
merged 1 commit into from
May 24, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/backends/sftp.rst
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,18 @@ static files) to use the sftp backend::
.. _`paramiko SSHClient.connect() documentation`: http://docs.paramiko.org/en/latest/api/client.html#paramiko.client.SSHClient.connect

.. _`Python os.chmod documentation`: http://docs.python.org/library/os.html#os.chmod


Standalone Use
--------------

If you intend to construct a storage instance not through Django but directly,
use the storage instance as a context manager to make sure the underlying SSH
connection is closed after use and no longer consumes resources.

.. code-block:: python

from storages.backends.sftpstorage import SFTPStorage

with SFTPStorage(...) as sftp:
sftp.listdir("")
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ google =
libcloud =
apache-libcloud
sftp =
paramiko >= 1.10.0
paramiko >= 1.15.0

[flake8]
exclude =
Expand Down
9 changes: 8 additions & 1 deletion storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
import paramiko
from django.core.files.base import File
from django.utils.deconstruct import deconstructible
from paramiko.util import ClosingContextManager

from storages.base import BaseStorage
from storages.utils import is_seekable
from storages.utils import setting


@deconstructible
class SFTPStorage(BaseStorage):
class SFTPStorage(ClosingContextManager, BaseStorage):
def __init__(self, **settings):
super().__init__(**settings)
self._host = self.host
Expand All @@ -35,6 +36,7 @@ def __init__(self, **settings):
self._known_host_file = self.known_host_file
self._root_path = self.root_path
self._base_url = self.base_url
self._ssh = None
self._sftp = None

def get_default_settings(self):
Expand Down Expand Up @@ -81,6 +83,11 @@ def _connect(self):
if self._ssh.get_transport():
self._sftp = self._ssh.open_sftp()

def close(self):
if self._ssh is None:
return
self._ssh.close()

@property
def sftp(self):
"""Lazy SFTP connection"""
Expand Down
12 changes: 12 additions & 0 deletions tests/test_sftp.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ def test_connect(self, mock_ssh):
self.storage._connect()
self.assertEqual('foo', mock_ssh.return_value.connect.call_args[0][0])

@patch('paramiko.SSHClient')
def test_close_unopened(self, mock_ssh):
with self.storage:
pass
mock_ssh.return_value.close.assert_not_called()

@patch('paramiko.SSHClient')
def test_close_opened(self, mock_ssh):
with self.storage as storage:
storage._connect()
mock_ssh.return_value.close.assert_called_once_with()

def test_open(self):
file_ = self.storage._open('foo')
self.assertIsInstance(file_, sftpstorage.SFTPStorageFile)
Expand Down