| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent add466d commit 358bf5f
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,7 +140,7 @@ def delete_blob(client, to_delete): | |||
| 140 | 140 | client = storage.Client() | |
| 141 | 141 | bucket = client.get_bucket('my-bucket') | |
| 142 | 142 | blobs = list(bucket.list_blobs()) | |
| 143 | - assert isinstance(blobs, list) | ||
| 143 | + assert len(blobs) > 0 | ||
| 144 | 144 | # [<Blob: my-bucket, my-file.txt>] | |
| 145 | 145 | bucket.delete_blob('my-file.txt') | |
| 146 | 146 | try: | |
@@ -223,6 +223,39 @@ def list_buckets(client, to_delete): | |||
| 223 | 223 | to_delete.append(bucket) | |
| 224 | 224 | ||
| 225 | 225 | ||
| 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 | + | ||
| 226 | 259 | def _line_no(func): | |
| 227 | 260 | code = getattr(func, '__code__', None) or getattr(func, 'func_code') | |
| 228 | 261 | return code.co_firstlineno | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -857,6 +857,12 @@ def generate_upload_policy( | |||
| 857 | 857 | `policy documents`_ to allow visitors to a website to upload files to | |
| 858 | 858 | Google Cloud Storage without giving them direct write access. | |
| 859 | 859 | ||
| 860 | + For example: | ||
| 861 | + | ||
| 862 | + .. literalinclude:: storage_snippets.py | ||
| 863 | + :start-after: [START policy_document] | ||
| 864 | + :end-before: [END policy_document] | ||
| 865 | + | ||
| 860 | 866 | .. _policy documents: | |
| 861 | 867 | https://cloud.google.com/storage/docs/xml-api\ | |
| 862 | 868 | /post-object#policydocument | |
| Back | FazBrowse Home | New Git URL |
0 commit comments