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

add get_modified_time for sftp storage #1347

Merged
merged 1 commit into from
Jul 6, 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
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,19 @@ Azure
- Add ``mode`` kwarg to ``.url()`` to support creation of signed URLs for upload (`#1414`_)
- Fix fetching user delegation key when custom domain is enabled (`#1418`_)

SFTP
----

- Add implementations of ``get_(modified|accessed)_time`` (`#1347`_)

.. _#1399: https://github.com/jschneier/django-storages/pull/1399
.. _#1381: https://github.com/jschneier/django-storages/pull/1381
.. _#1402: https://github.com/jschneier/django-storages/pull/1402
.. _#1403: https://github.com/jschneier/django-storages/pull/1403
.. _#1414: https://github.com/jschneier/django-storages/pull/1414
.. _#1417: https://github.com/jschneier/django-storages/pull/1417
.. _#1418: https://github.com/jschneier/django-storages/pull/1418
.. _#1347: https://github.com/jschneier/django-storages/pull/1347


1.14.3 (2024-05-04)
Expand Down
14 changes: 14 additions & 0 deletions storages/backends/sftpstorage.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import os
import posixpath
import stat
from datetime import datetime
from urllib.parse import urljoin

import paramiko
from django.core.files.base import File
from django.utils import timezone
from django.utils.deconstruct import deconstructible
from paramiko.util import ClosingContextManager

Expand Down Expand Up @@ -190,6 +192,18 @@ def size(self, name):
remote_path = self._remote_path(name)
return self.sftp.stat(remote_path).st_size

def get_accessed_time(self, name):
remote_path = self._remote_path(name)
utime = self.sftp.stat(remote_path).st_atime
ts = datetime.fromtimestamp(utime)
return timezone.make_aware(ts) if setting("USE_TZ") else ts

def get_modified_time(self, name):
remote_path = self._remote_path(name)
utime = self.sftp.stat(remote_path).st_mtime
ts = datetime.fromtimestamp(utime)
return timezone.make_aware(ts) if setting("USE_TZ") else ts

def url(self, name):
if self._base_url is None:
raise ValueError("This file is not accessible via a URL.")
Expand Down
Loading