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

[storage] feat: add post policy sample #3231

Merged
merged 6 commits into from
Apr 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion storage/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
google-cloud-pubsub==1.1.0
google-cloud-storage==1.26.0
google-cloud-storage==1.27.0
17 changes: 17 additions & 0 deletions storage/cloud-client/snippets_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import storage_generate_signed_url_v2
import storage_generate_signed_url_v4
import storage_generate_upload_signed_url_v4
import storage_generate_signed_post_policy_v4
import storage_set_bucket_default_kms_key

KMS_KEY = os.environ["CLOUD_KMS_KEY"]
Expand Down Expand Up @@ -202,6 +203,22 @@ def test_generate_upload_signed_url_v4(test_bucket, capsys):
assert blob.download_as_string() == content


def test_generate_signed_policy_v4(test_bucket, capsys):
blob_name = "storage_snippets_test_form"
short_name = storage_generate_signed_post_policy_v4
form = short_name.generate_signed_post_policy_v4(
test_bucket.name, blob_name
)
assert "name='key' value='{}'".format(blob_name) in form
assert "name='x-goog-signature'" in form
assert "name='x-goog-date'" in form
assert "name='x-goog-credential'" in form
assert "name='x-goog-algorithm' value='GOOG4-RSA-SHA256'" in form
assert "name='policy'" in form
assert "name='x-goog-meta-test' value='data'" in form
assert "type='file' name='file'/>" in form


def test_rename_blob(test_blob):
bucket = storage.Client().bucket(test_blob.bucket.name)

Expand Down
62 changes: 62 additions & 0 deletions storage/cloud-client/storage_generate_signed_post_policy_v4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env python

# Copyright 2020 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the 'License');
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import sys

# [START storage_generate_signed_post_policy_v4]
from google.cloud import storage
import datetime


def generate_signed_post_policy_v4(bucket_name, blob_name):
"""Generates a v4 POST Policy and prints an HTML form."""
# bucket_name = 'your-bucket-name'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do this in other samples also, but it seems odd to me that these aren't just documents in the docstring of the method. For instance https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/storage/cloud-client/storage_create_bucket.py#L25

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the way we've done it for a while now but open to changing it. IIUC it's part of sample expectations for Python and was approved.

# blob_name = 'your-object-name'

storage_client = storage.Client()

policy = storage_client.generate_signed_post_policy_v4(
bucket_name,
blob_name,
expiration=datetime.timedelta(minutes=10),
fields={
'x-goog-meta-test': 'data'
}
)

# Create an HTML form with the provided policy
header = "<form action='{}' method='POST' enctype='multipart/form-data'>\n"
form = header.format(policy["url"])

# Include all fields returned in the HTML form as they're required
for index, (key, value) in enumerate(policy["fields"].items()):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for key, value in policy["fields"].items():

form += " <input name='{}' value='{}' type='hidden'/>\n".format(key, value)

form += " <input type='file' name='file'/><br />\n"
form += " <input type='submit' value='Upload File' name='submit'/><br />\n"
form += "</form>"

print(form)

return form


# [END storage_generate_signed_post_policy_v4]

if __name__ == "__main__":
generate_signed_post_policy_v4(
bucket_name=sys.argv[1], blob_name=sys.argv[2]
)