FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Add policy document example and update assertion. · googleapis/google-cloud-python@358bf5f · GitHub

Commit 358bf5f

Browse files
committed
Add policy document example and update assertion.
1 parent add466d commit 358bf5f

2 files changed

Lines changed: 40 additions & 1 deletion

File tree

‎docs/storage_snippets.py‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def delete_blob(client, to_delete):
140140
client = storage.Client()
141141
bucket = client.get_bucket('my-bucket')
142142
blobs = list(bucket.list_blobs())
143-
assert isinstance(blobs, list)
143+
assert len(blobs) > 0
144144
# [<Blob: my-bucket, my-file.txt>]
145145
bucket.delete_blob('my-file.txt')
146146
try:
@@ -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="">'
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
@@ -857,6 +857,12 @@ def generate_upload_policy(
857857
`policy documents`_ to allow visitors to a website to upload files to
858858
Google Cloud Storage without giving them direct write access.
859859
860+
For example:
861+
862+
.. literalinclude:: storage_snippets.py
863+
:start-after: [START policy_document]
864+
:end-before: [END policy_document]
865+
860866
.. _policy documents:
861867
https://cloud.google.com/storage/docs/xml-api\
862868
/post-object#policydocument

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL