Description
Feature request
Use Case
Enable direct download of files stored using the GoogleCloudStorage
class instead of viewing inline. This can be extended to a broader issue of having more control over the URL generation mechanism.
Current state
When we click on a URL generated for a file stored using GoogleCloudStorage, we navigate to the file instead of immediately triggering a download. This is the right behavior when we're using the file in code instead of downloading from a browser.
Proposed Solution
When generating the signed URL, we can pass the response_disposition
argument or all kwargs through to generate_signed_url
.
Passing the response_disposition 'attachment' (you can also add the filename here, but I don't use that) makes it a direct download link.
blob.generate_signed_url(expiration=self.settings.expiration, response_disposition="attachment", version="v4")
class GoogleCloudStorage(CompressStorageMixin, Storage):
def url(self, name, **kwargs):
"""
Return public url or a signed url for the Blob.
This DOES NOT check for existance of Blob - that makes codes too slow
for many use cases.
"""
name = self._normalize_name(clean_name(name))
blob = self.bucket.blob(name)
blob_params = self.get_object_parameters(name)
no_signed_url = (
blob_params.get("acl", self.settings.default_acl) == "publicRead" or not self.settings.querystring_auth
)
if not self.settings.custom_endpoint and no_signed_url:
return blob.public_url
elif no_signed_url:
return "{storage_base_url}/{quoted_name}".format(
storage_base_url=self.settings.custom_endpoint,
quoted_name=_quote(name, safe=b"/~"),
)
elif not self.settings.custom_endpoint:
# Pass kwarg into the method
return blob.generate_signed_url(expiration=self.settings.expiration, version="v4", **kwargs)
else:
# Pass kwarg into the method
return blob.generate_signed_url(
bucket_bound_hostname=self.settings.custom_endpoint,
expiration=self.settings.expiration,
version="v4",
**kwargs
)
This could also be a setting