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 functionality for content-disposition option in signed url. #253

Merged
merged 1 commit into from
Aug 3, 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
3 changes: 3 additions & 0 deletions src/gcp_storage_emulator/handlers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@ def download(request, response, storage, *args, **kwargs):
hash_header = "crc32c={},md5={}".format(obj["crc32c"], obj["md5Hash"])
response[_HASH_HEADER] = hash_header

if 'response-content-disposition' in request.query:
response['Content-Disposition'] = request.query['response-content-disposition'][0]

response.write_file(file, content_type=obj.get("contentType"))
except NotFound:
response.status = HTTPStatus.NOT_FOUND
Expand Down
59 changes: 59 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,6 +810,65 @@ def test_signed_url_download(self):
self.assertEqual(response.content, content)
self.assertEqual(response.headers["content-type"], "text/mycustom")

def test_signed_url_download_with_content_disposition(self):
content = b"The quick brown fox jumps over the lazy dog"
bucket = self._client.create_bucket("testbucket")

blob = bucket.blob("signed-download")
blob.upload_from_string(content, content_type="text/mycustom")

requested_filename = 'requested_filename.cst2'
response_disposition = f'attachment; filename="{requested_filename}"'

url = blob.generate_signed_url(
api_access_endpoint="http://localhost:9023",
credentials=FakeSigningCredentials(),
version="v4",
expiration=datetime.timedelta(minutes=15),
response_disposition=response_disposition,
method="GET",
)

response = requests.get(url)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, content)
self.assertEqual(response.headers['content-disposition'], f"{response_disposition}")
self.assertEqual(response.headers["content-type"], "text/mycustom")

def test_url_generation_for_browser(self):
self.skipTest('Used to test browser functionality with URL, not API.')
os.environ["STORAGE_EMULATOR_HOST"] = "http://localhost:8080"
content = b"The quick brown fox jumps over the lazy dog"

# Cloud Storage uses environment variables to configure API endpoints for
# file upload - which is read at module import time
from google.cloud import storage
http = requests.Session()

client = storage.Client(
project="[PROJECT]",
_http=http,
client_options={"api_endpoint": "http://localhost:8080"},
)

bucket = client.create_bucket("testbucket")

blob = bucket.blob("signed-download")
blob.upload_from_string(content, content_type="text/html")

requested_filename = 'requested_filename.cst2'
response_disposition = f'attachment; filename="{requested_filename}"'

url = blob.generate_signed_url(
api_access_endpoint="http://localhost:8080",
credentials=FakeSigningCredentials(),
version="v4",
expiration=datetime.timedelta(minutes=15),
response_disposition=response_disposition,
method="GET",
)
print(url)

def test_signed_url_upload(self):
bucket = self._client.create_bucket("testbucket")
blob = bucket.blob("signed-upload")
Expand Down
Loading