-
-
Save daspecster/8735dcb1d8139e99eaf4bb7fc2fc28f9 to your computer and use it in GitHub Desktop.
Storage Snippets Results
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import google | |
from google.cloud import storage | |
client = storage.Client() | |
# bucket = client.create_bucket('my-bucket24356uytrew') | |
# assert isinstance(bucket, storage.Bucket) | |
""" | |
Output: | |
""" | |
# try: | |
# bucket = client.get_bucket('my-bucke23r2fqwft') | |
# except google.cloud.exceptions.NotFound: | |
# print('Sorry, that bucket does not exist!') | |
""" | |
Output: | |
Sorry, that bucket does not exist! | |
""" | |
# for bucket in client.list_buckets(): | |
# print(bucket) | |
""" | |
Output: | |
<Bucket: ferrous-arena-my-test-bucket> | |
<Bucket: my-bucket24356uytrew> | |
<Bucket: persistent-storage-snippet-bucket> | |
<Bucket: testing-secure-bucket> | |
""" | |
# bucket = client.lookup_bucket('doesnt-exist') | |
# assert not bucket | |
# # None | |
# bucket = client.lookup_bucket('my-bucket24356uytrew') | |
# assert isinstance(bucket, storage.Bucket) | |
# # <Bucket: my-bucket> | |
""" | |
Output: | |
""" | |
from google.cloud.storage import Blob | |
# | |
# client = storage.Client(project='my-project') | |
# bucket = client.get_bucket('my-bucket24356uytrew') | |
# encryption_key = 'c7f32af42e45e85b9848a6a14dd2a8f6' | |
# blob = Blob('secure-data', bucket, encryption_key=encryption_key) | |
# blob.upload_from_string('my secret message') | |
# | |
# with open('test_encryption.txt', 'wb') as file_obj: | |
# blob.download_to_file(file_obj) | |
""" | |
Output: | |
$ cat test_encryption.txt | |
my secret message | |
""" | |
# | |
# client = storage.Client(project='my-project') | |
# bucket = client.get_bucket('my-bucket24356uytrew') | |
# encryption_key = 'aa426195405adee2c8081bb9e7e74b19' | |
# blob = Blob('secure-data-upload', bucket, encryption_key=encryption_key) | |
# with open('test_encryption.txt', 'rb') as my_file: | |
# blob.upload_from_file(my_file) | |
# | |
""" | |
Output: | |
checked via console. OK | |
""" | |
# bucket = client.get_bucket('my-bucket24356uytrew') | |
# | |
# blob = Blob('index.html', bucket) | |
# blob.upload_from_string('<h1>testing</h1>') | |
# | |
# blob = Blob('404.html', bucket) | |
# blob.upload_from_string('<h1>404ed</h1>') | |
# | |
# bucket.configure_website('index.html', '404.html') | |
# bucket.make_public(recursive=True, future=True) | |
""" | |
Output: | |
None # checked via console, public site checked. OK | |
""" | |
# | |
# from google.cloud.exceptions import NotFound | |
# | |
# client = storage.Client() | |
# bucket = client.get_bucket('my-bucket24356uytrew') | |
# blobs = list(bucket.list_blobs()) | |
# assert isinstance(blobs, list) | |
# # [<Blob: my-bucket, my-file.txt>] | |
# bucket.delete_blob('404.html') | |
# try: | |
# bucket.delete_blob('doesnt-exist') | |
# except NotFound: | |
# pass | |
# | |
# blob = Blob('index.html', bucket) | |
# bucket.delete_blobs([blob], on_error=lambda blob: None) | |
""" | |
Output: | |
Checked via console. blobs removed. OK | |
""" | |
# bucket = client.bucket('my-bucket24356uytrew') | |
# 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="">' | |
# '<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) | |
""" | |
Output: | |
html form. Broken | |
<Error> | |
<Code>InvalidArgument</Code> | |
<Message>Invalid argument.</Message> | |
<Details>Cannot create buckets using a POST.</Details> | |
</Error> | |
""" | |
# | |
# client = storage.Client() | |
# bucket = client.get_bucket('my-bucket24356uytrew') | |
# assert isinstance(bucket.get_blob('secure-data'), Blob) | |
# # <Blob: my-bucket, /path/to/blob.txt> | |
# assert not bucket.get_blob('/does-not-exist.txt') | |
# # None | |
""" | |
Output: | |
""" | |
client = storage.Client() | |
bucket = client.get_bucket('my-bucket24356uytrew') | |
acl = bucket.acl | |
print(acl) | |
""" | |
Output: | |
<google.cloud.storage.acl.BucketACL object at 0x10cc4ef90> | |
""" | |
acl.user('redacted@redacted.com').grant_read() | |
acl.all_authenticated().grant_write() | |
acl.all().grant_read() | |
acl.all().revoke_write() | |
acl.save() | |
bucket.acl.save(acl=acl) | |
print(list(acl)) | |
""" | |
Output: | |
<google.cloud.storage.acl.BucketACL object at 0x107967a90> | |
[{'role': u'READER', 'entity': 'project-viewers-226859677163'}, {'role': u'READER', 'entity': 'allUsers'}, {'role': u'OWNER', 'entity': 'project-owners-226859677163'}, {'role': u'READER', 'entity': 'user-redacted@redacted.com'}, {'role': u'WRITER', 'entity': 'allAuthenticatedUsers'}, {'role': u'OWNER', 'entity': 'project-editors-226859677163'}] | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment