Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add policy_document snippet back. #3231

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions docs/storage_snippets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<input type="hidden" name="{key}" value="{value}">'.format(
key=key, value=value)
for key, value in policy.items()
)

upload_form = (
'<form action="http://{bucket_name}.storage.googleapis.com"'
' method="post" enctype="multipart/form-data">'
'<input type="text" name="key" value="my-test-key">'
'<input type="hidden" name="bucket" value="{bucket_name}">'
'<input type="hidden" name="acl" value="public-read">'
'<input name="file" type="file">'
'<input type="submit" value="Upload">'
'{policy_fields}'
'</form>').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
Expand Down
6 changes: 6 additions & 0 deletions storage/google/cloud/storage/bucket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down