Skip to content

Commit

Permalink
[storage] feat: add post policy sample (#3231)
Browse files Browse the repository at this point in the history
* feat: add post policy sample

* use 1.27.0

* fix

* simplify iterator

Co-authored-by: Jonathan Lui <jonathanlui@google.com>
  • Loading branch information
frankyn and jkwlui authored Apr 3, 2020
1 parent eb6a7a2 commit ba4d402
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
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'
# 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 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]
)

0 comments on commit ba4d402

Please sign in to comment.