Skip to content

Commit 0950085

Browse files
committed
Add policy_document snippet back.
1 parent 20fcf40 commit 0950085

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

docs/storage_snippets.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,39 @@ def list_buckets(client, to_delete):
223223
to_delete.append(bucket)
224224

225225

226+
@snippet
227+
def policy_document(client, to_delete):
228+
# pylint: disable=unused-argument
229+
# [START policy_document]
230+
bucket = client.bucket('my-bucket')
231+
conditions = [
232+
['starts-with', '$key', ''],
233+
{'acl': 'public-read'}]
234+
235+
policy = bucket.generate_upload_policy(conditions)
236+
237+
# Generate an upload form using the form fields.
238+
policy_fields = ''.join(
239+
'<input type="hidden" name="{key}" value="{value}">'.format(
240+
key=key, value=value)
241+
for key, value in policy.items()
242+
)
243+
244+
upload_form = (
245+
'<form action="http://{bucket_name}.storage.googleapis.com"'
246+
' method="post" enctype="multipart/form-data">'
247+
'<input type="text" name="key" value="my-test-key">'
248+
'<input type="hidden" name="bucket" value="{bucket_name}">'
249+
'<input type="hidden" name="acl" value="public-read">'
250+
'<input name="file" type="file">'
251+
'<input type="submit" value="Upload">'
252+
'{policy_fields}'
253+
'</form>').format(bucket_name=bucket.name, policy_fields=policy_fields)
254+
255+
print(upload_form)
256+
# [END policy_document]
257+
258+
226259
def _line_no(func):
227260
code = getattr(func, '__code__', None) or getattr(func, 'func_code')
228261
return code.co_firstlineno

storage/google/cloud/storage/bucket.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,12 @@ def generate_upload_policy(
859859
`policy documents`_ to allow visitors to a website to upload files to
860860
Google Cloud Storage without giving them direct write access.
861861
862+
For example:
863+
864+
.. literalinclude:: storage_snippets.py
865+
:start-after: [START policy_document]
866+
:end-before: [END policy_document]
867+
862868
.. _policy documents:
863869
https://cloud.google.com/storage/docs/xml-api\
864870
/post-object#policydocument

0 commit comments

Comments
 (0)