marazmiki/django-selectel-storage

401 Client Error: Unauthorized for url

Opened this issue · 6 comments

Hi. I'm using your storage as a default file storage. I'm reading file content in some celery tasks and everything works ok, but after some moment I'm starting to get HTTP 401 Error. After restarting celery the error disappears but on the next day it's come back.
It's looks like your Auth class for some reason doesn't reauthenticate after token expired. It's weird because the container is public and file content can be accessed without authentication.

Stacktrace (most recent call last):

  File "mail/tasks.py", line 46, in send_email
    content = attachment.file.read()
  File "django/core/files/utils.py", line 16, in <lambda>
    read = property(lambda self: self.file.read)
  File "django/db/models/fields/files.py", line 43, in _get_file
    self._file = self.storage.open(self.name, 'rb')
  File "queued_storage/backends.py", line 172, in open
    return self.get_storage(name).open(name, mode)
  File "django/core/files/storage.py", line 36, in open
    return self._open(name, mode)
  File "django_selectel_storage/storage.py", line 13, in _open
    return base.ContentFile(self.container.open(name).read())
  File "django_selectel_storage/selectel.py", line 110, in open
    stream=True).raw
  File "django_selectel_storage/selectel.py", line 106, in perform_request
    **kwargs)
  File "django_selectel_storage/selectel.py", line 79, in perform_request
    resp.raise_for_status()
  File "requests/models.py", line 941, in raise_for_status
    raise HTTPError(http_error_msg, response=self)

401 Client Error: Unauthorized for url: https://144411.selcdn.ru/3dplitka_media/uploads/organizations/requisites/Hafizova.docx

I'm using Python 3.7.6 with Django 2.2.12 and django-selectel-storage 1.0.0

Suppose, you're right in your point about it could not refresh the token. Gonna research it deeply.

It's weird because the container is public and file content can be accessed without authentication.

I think it's because the token is wrong. If you provide the correct one or just drop it off, it would work as expected. At least, I clicked on the link from the traceback and got a .docx file

Thanks for a fast reply. Looking forward for a fix, now I'm just catching 401 exceptions and reauthenticate your storage manually but it's a dirty way.

Well, if it works, who cares? :)

BTW... Maybe if you have some time for that, can you please fork the app and put some loggers there to get what's happening there? What data is sent, timestamps, etc.

After all, you have a working stand reproducing an error :) Not sure if I can repeat it fast.

Well, if it works, who cares? :)

My inner perfectionist :) I'll try to investigate this problem deeper in the next days and send you a pull request with fix

After some debugging I didn't found any reason why I'm getting those 401 errors. So I just added a check to reauthenticate and repeat request if needed.