django-backblaze-b2 icon indicating copy to clipboard operation
django-backblaze-b2 copied to clipboard

Authorization token doesn't automatically renew after expiration

Open anderoonies opened this issue 5 months ago • 5 comments

I'm using a subclass django-backblaze-b2, with the following configuration:

BACKBLAZE_CONFIG = {
    "account_info": {"type": "django-cache", "cache": "django-backblaze-b2"},
    "application_key_id": os.getenv("BACKBLAZE_KEY_ID", None),
    "application_key": os.getenv("BACKBLAZE_KEY", None),
    "bucket": "<bucket name>",
}

CACHES = {
    'django-backblaze-b2': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'},
}

I'm also subclassing BackblazeB2Storage to add the following:

@deconstructible
class BackblazeB2StorageCDN(BackblazeB2Storage):
    
    def _get_file_url(self, name: str) -> str:
        if getattr(settings, 'BACKBLAZE_URL_CACHE', True):
            b2_url = cache.get(name)

            if not b2_url:
                b2_url = self.get_backblaze_url(name)
                cache.set(name, b2_url, 3600)

        else:
            b2_url = self.get_backblaze_url(name)

        if getattr(settings, 'BACKBLAZE_CDN_URL', None):
            b2_download_url = urlparse(b2_url)
            cdn_url = urlparse(settings.BACKBLAZE_CDN_URL)

            file_path = Path(b2_download_url.path)
            file_path = str(Path(*file_path.parts[3:]))
            cdn_url = cdn_url._replace(path=file_path)
            return cdn_url.geturl()
        else:
            return b2_url

After 24 hours, presumably when the B2 authorizationToken expires, I start seeing these errors: couldn't save the photo: unauthorized for application key with capabilities 'listBuckets,writeBuc...

When erroring, b2sdk makes a request to b2_authorize_account, then errors on a HEAD request to upload the file.

Based on that, it seems like this is happening within the authorization loop of b2sdk.

Is there any issue with my configuration that is resulting in the new authorizatoinToken not saving in the cache?

anderoonies avatar Sep 07 '24 21:09 anderoonies