From 0950085dbf6df2a628aa13e772eaba7e52fad577 Mon Sep 17 00:00:00 2001 From: daspecster Date: Tue, 28 Mar 2017 17:44:54 -0400 Subject: [PATCH] Add policy_document snippet back. --- docs/storage_snippets.py | 33 ++++++++++++++++++++++++++ storage/google/cloud/storage/bucket.py | 6 +++++ 2 files changed, 39 insertions(+) diff --git a/docs/storage_snippets.py b/docs/storage_snippets.py index f41629dff5a0..ff8b51694276 100644 --- a/docs/storage_snippets.py +++ b/docs/storage_snippets.py @@ -223,6 +223,39 @@ def list_buckets(client, to_delete): to_delete.append(bucket) +@snippet +def policy_document(client, to_delete): + # pylint: disable=unused-argument + # [START policy_document] + bucket = client.bucket('my-bucket') + conditions = [ + ['starts-with', '$key', ''], + {'acl': 'public-read'}] + + policy = bucket.generate_upload_policy(conditions) + + # Generate an upload form using the form fields. + policy_fields = ''.join( + ''.format( + key=key, value=value) + for key, value in policy.items() + ) + + upload_form = ( + '
' + '' + '' + '' + '' + '' + '{policy_fields}' + '
').format(bucket_name=bucket.name, policy_fields=policy_fields) + + print(upload_form) + # [END policy_document] + + def _line_no(func): code = getattr(func, '__code__', None) or getattr(func, 'func_code') return code.co_firstlineno diff --git a/storage/google/cloud/storage/bucket.py b/storage/google/cloud/storage/bucket.py index 38fbadce368d..819b273b77d1 100644 --- a/storage/google/cloud/storage/bucket.py +++ b/storage/google/cloud/storage/bucket.py @@ -859,6 +859,12 @@ def generate_upload_policy( `policy documents`_ to allow visitors to a website to upload files to Google Cloud Storage without giving them direct write access. + For example: + + .. literalinclude:: storage_snippets.py + :start-after: [START policy_document] + :end-before: [END policy_document] + .. _policy documents: https://cloud.google.com/storage/docs/xml-api\ /post-object#policydocument