etianen / django-s3-storage

Django Amazon S3 file storage.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to delete record which used django-s3-storge

sunjiali opened this issue · comments

instance = Video.objects.get(id=video_id)
instance.delete()

ERROR:

/storage.py", line 123, in path
raise NotImplementedError("This backend doesn't support absolute paths.")
NotImplementedError: This backend doesn't support absolute paths.

Can I have the complete traceback, please? It looks like something in your code is expecting to be able to get a filesystem path. Since S3 doesn't support filesystem paths, that's not going to work. There's nothing in Django or django-s3-storage that mandates filesystem paths, so this is likely to be an issue in your code, or some 3rd party code. It's likely fixable, as S3 storage supports deletions just fine.

On Sun, 18 Jul 2021 at 13:03, sunjiali @.***> wrote: instance = Video.objects.get(id=video_id) instance.delete() ERROR: /storage.py", line 123, in path raise NotImplementedError("This backend doesn't support absolute paths.") NotImplementedError: This backend doesn't support absolute paths. — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub <#125>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABEKCE6YRGRBKCGFVR2FP3TYK7KHANCNFSM5ASCLWPA .

Here is complete

/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1361: RuntimeWarning: DateTimeField AmazonUser.date_joined received a naive datetime (2021-07-27 00:00:00) while time zone support is active. RuntimeWarning) /local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/fields/__init__.py:1361: RuntimeWarning: DateTimeField Comment.timestamp received a naive datetime (2021-07-27 00:00:00) while time zone support is active. RuntimeWarning) Admin dispatch Admin dispatch 2021-07-27T12:57:40.290409+00:00 zhytoolsset 361-140621172176640@dev-dsk-sunjiali-2c-1a5c57ee.us-west-2.amazon.com:0 [ERROR] log.py:230 log_response(): log: Internal Server Error: /center/video_delete/ Traceback (most recent call last): File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/video/helpers.py", line 46, in wrap return f(request, *args, **kwargs) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner return func(request, *args, **kwargs) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/myadmin/views.py", line 154, in video_delete instance.delete() File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/base.py", line 954, in delete return collector.delete() File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/deletion.py", line 436, in delete sender=model, instance=obj, using=self.using File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 182, in send for receiver in self._live_receivers(sender) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp> for receiver in self._live_receivers(sender) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/video/models.py", line 118, in auto_delete_file_on_delete if os.path.isfile(instance.file.path): File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/fields/files.py", line 59, in path return self.storage.path(self.name) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/files/storage.py", line 123, in path raise NotImplementedError("This backend doesn't support absolute paths.") NotImplementedError: This backend doesn't support absolute paths. Internal Server Error: /center/video_delete/ Traceback (most recent call last): File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/handlers/exception.py", line 47, in inner response = get_response(request) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/handlers/base.py", line 181, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/video/helpers.py", line 46, in wrap return f(request, *args, **kwargs) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner return func(request, *args, **kwargs) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/myadmin/views.py", line 154, in video_delete instance.delete() File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/base.py", line 954, in delete return collector.delete() File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/deletion.py", line 436, in delete sender=model, instance=obj, using=self.using File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 182, in send for receiver in self._live_receivers(sender) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 182, in <listcomp> for receiver in self._live_receivers(sender) File "/local/home/sunjiali/RDEBonesLambda/src/ZHYToolsSet/video/models.py", line 118, in auto_delete_file_on_delete if os.path.isfile(instance.file.path): File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/db/models/fields/files.py", line 59, in path return self.storage.path(self.name) File "/local/home/sunjiali/RDEBonesLambda/env/ZHYToolsSet-1.0/test-runtime/lib/python3.6/site-packages/django/core/files/storage.py", line 123, in path raise NotImplementedError("This backend doesn't support absolute paths.") NotImplementedError: This backend doesn't support absolute paths.

The problem is in ZHYToolsSet/video/models.py where you do os.path.isfile(instance.file.path). You cannot access file.path for django-s3-storage.

I'm not sure what this check acheives, as all S3 files are files. You should be able to simply:

from django.core.files.storage import default_storage

default_storage.delete(file.name)