| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 113a50f commit 2a2cc57
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -91,7 +91,7 @@ Python built-in ``object``. | |||
| 91 | 91 | If you want to set some data, you just create a ``Blob`` inside your bucket | |
| 92 | 92 | and store your data inside the blob:: | |
| 93 | 93 | ||
| 94 | - >>> blob = storage.Blob('greeting.txt', bucket=bucket) | ||
| 94 | + >>> blob = bucket.blob('greeting.txt') | ||
| 95 | 95 | >>> blob.upload_from_string('Hello world!') | |
| 96 | 96 | ||
| 97 | 97 | This creates a :class:`Blob <gcloud.storage.blob.Blob>` object locally and | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -61,7 +61,7 @@ you can create buckets and blobs:: | |||
| 61 | 61 | >>> bucket = client.create_bucket('my-new-bucket') | |
| 62 | 62 | >>> print bucket | |
| 63 | 63 | <Bucket: my-new-bucket> | |
| 64 | - >>> blob = storage.Blob('my-test-file.txt', bucket=bucket) | ||
| 64 | + >>> blob = bucket.blob('my-test-file.txt') | ||
| 65 | 65 | >>> print blob | |
| 66 | 66 | <Blob: my-new-bucket, my-test-file.txt> | |
| 67 | 67 | >>> blob = blob.upload_from_string('this is test content!') | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -54,5 +54,5 @@ Cloud Storage | |||
| 54 | 54 | from gcloud import storage | |
| 55 | 55 | client = storage.Client() | |
| 56 | 56 | bucket = client.get_bucket('<your-bucket-name>') | |
| 57 | - blob = storage.Blob('my-test-file.txt', bucket=bucket) | ||
| 58 | - blob = blob.upload_contents_from_string('this is test content!') | ||
| 57 | + blob = bucket.blob('my-test-file.txt') | ||
| 58 | + blob = blob.upload_from_string('this is test content!') | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -99,6 +99,26 @@ def __init__(self, client, name=None): | |||
| 99 | 99 | def __repr__(self): | |
| 100 | 100 | return '<Bucket: %s>' % self.name | |
| 101 | 101 | ||
| 102 | + def blob(self, blob_name, chunk_size=None): | ||
| 103 | + """Factory constructor for blob object. | ||
| 104 | + | ||
| 105 | + .. note:: | ||
| 106 | + This will not make an HTTP request; it simply instantiates | ||
| 107 | + a blob object owned by this bucket. | ||
| 108 | + | ||
| 109 | + :type blob_name: string | ||
| 110 | + :param blob_name: The name of the blob to be instantiated. | ||
| 111 | + | ||
| 112 | + :type chunk_size: integer | ||
| 113 | + :param chunk_size: The size of a chunk of data whenever iterating | ||
| 114 | + (1 MB). This must be a multiple of 256 KB per the | ||
| 115 | + API specification. | ||
| 116 | + | ||
| 117 | + :rtype: :class:`gcloud.storage.blob.Blob` | ||
| 118 | + :returns: The blob object created. | ||
| 119 | + """ | ||
| 120 | + return Blob(name=blob_name, bucket=self, chunk_size=chunk_size) | ||
| 121 | + | ||
| 102 | 122 | def exists(self, client=None): | |
| 103 | 123 | """Determines whether or not this bucket exists. | |
| 104 | 124 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,6 +19,7 @@ | |||
| 19 | 19 | from gcloud.client import JSONClient | |
| 20 | 20 | from gcloud.exceptions import NotFound | |
| 21 | 21 | from gcloud.iterator import Iterator | |
| 22 | + from gcloud.storage.batch import Batch | ||
| 22 | 23 | from gcloud.storage.bucket import Bucket | |
| 23 | 24 | from gcloud.storage.connection import Connection | |
| 24 | 25 | ||
@@ -113,6 +114,33 @@ def current_batch(self): | |||
| 113 | 114 | """ | |
| 114 | 115 | return self._batch_stack.top | |
| 115 | 116 | ||
| 117 | + def bucket(self, bucket_name): | ||
| 118 | + """Factory constructor for bucket object. | ||
| 119 | + | ||
| 120 | + .. note:: | ||
| 121 | + This will not make an HTTP request; it simply instantiates | ||
| 122 | + a bucket object owned by this client. | ||
| 123 | + | ||
| 124 | + :type bucket_name: string | ||
| 125 | + :param bucket_name: The name of the bucket to be instantiated. | ||
| 126 | + | ||
| 127 | + :rtype: :class:`gcloud.storage.bucket.Bucket` | ||
| 128 | + :returns: The bucket object created. | ||
| 129 | + """ | ||
| 130 | + return Bucket(client=self, name=bucket_name) | ||
| 131 | + | ||
| 132 | + def batch(self): | ||
| 133 | + """Factory constructor for batch object. | ||
| 134 | + | ||
| 135 | + .. note:: | ||
| 136 | + This will not make an HTTP request; it simply instantiates | ||
| 137 | + a batch object owned by this client. | ||
| 138 | + | ||
| 139 | + :rtype: :class:`gcloud.storage.batch.Batch` | ||
| 140 | + :returns: The batch object created. | ||
| 141 | + """ | ||
| 142 | + return Batch(client=self) | ||
| 143 | + | ||
| 116 | 144 | def get_bucket(self, bucket_name): | |
| 117 | 145 | """Get a bucket by name. | |
| 118 | 146 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ | |||
| 37 | 37 | print(list(client.list_buckets())) | |
| 38 | 38 | ||
| 39 | 39 | # How about we create a new blob inside this bucket. | |
| 40 | - blob = storage.Blob("my-new-file.txt", bucket=bucket) | ||
| 40 | + blob = bucket.blob("my-new-file.txt") | ||
| 41 | 41 | ||
| 42 | 42 | # Now let's put some data in there. | |
| 43 | 43 | blob.upload_from_string("this is some data!") | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -109,6 +109,19 @@ def test_ctor(self): | |||
| 109 | 109 | self.assertFalse(bucket._default_object_acl.loaded) | |
| 110 | 110 | self.assertTrue(bucket._default_object_acl.bucket is bucket) | |
| 111 | 111 | ||
| 112 | + def test_blob(self): | ||
| 113 | + from gcloud.storage.blob import Blob | ||
| 114 | + | ||
| 115 | + BUCKET_NAME = 'BUCKET_NAME' | ||
| 116 | + BLOB_NAME = 'BLOB_NAME' | ||
| 117 | + | ||
| 118 | + bucket = self._makeOne(name=BUCKET_NAME) | ||
| 119 | + blob = bucket.blob(BLOB_NAME) | ||
| 120 | + self.assertTrue(isinstance(blob, Blob)) | ||
| 121 | + self.assertTrue(blob.bucket is bucket) | ||
| 122 | + self.assertTrue(blob.client is bucket.client) | ||
| 123 | + self.assertEqual(blob.name, BLOB_NAME) | ||
| 124 | + | ||
| 112 | 125 | def test_exists_miss(self): | |
| 113 | 126 | from gcloud.exceptions import NotFound | |
| 114 | 127 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -89,6 +89,30 @@ def test_connection_getter_with_batch(self): | |||
| 89 | 89 | self.assertTrue(client.connection is batch) | |
| 90 | 90 | self.assertTrue(client.current_batch is batch) | |
| 91 | 91 | ||
| 92 | + def test_bucket(self): | ||
| 93 | + from gcloud.storage.bucket import Bucket | ||
| 94 | + | ||
| 95 | + PROJECT = object() | ||
| 96 | + CREDENTIALS = _Credentials() | ||
| 97 | + BUCKET_NAME = 'BUCKET_NAME' | ||
| 98 | + | ||
| 99 | + client = self._makeOne(project=PROJECT, credentials=CREDENTIALS) | ||
| 100 | + bucket = client.bucket(BUCKET_NAME) | ||
| 101 | + self.assertTrue(isinstance(bucket, Bucket)) | ||
| 102 | + self.assertTrue(bucket.client is client) | ||
| 103 | + self.assertEqual(bucket.name, BUCKET_NAME) | ||
| 104 | + | ||
| 105 | + def test_batch(self): | ||
| 106 | + from gcloud.storage.batch import Batch | ||
| 107 | + | ||
| 108 | + PROJECT = object() | ||
| 109 | + CREDENTIALS = _Credentials() | ||
| 110 | + | ||
| 111 | + client = self._makeOne(project=PROJECT, credentials=CREDENTIALS) | ||
| 112 | + batch = client.batch() | ||
| 113 | + self.assertTrue(isinstance(batch, Batch)) | ||
| 114 | + self.assertTrue(batch._client is client) | ||
| 115 | + | ||
| 92 | 116 | def test_get_bucket_miss(self): | |
| 93 | 117 | from gcloud.exceptions import NotFound | |
| 94 | 118 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -51,9 +51,9 @@ def setUp(self): | |||
| 51 | 51 | self.case_buckets_to_delete = [] | |
| 52 | 52 | ||
| 53 | 53 | def tearDown(self): | |
| 54 | - with storage.Batch(CLIENT): | ||
| 54 | + with CLIENT.batch(): | ||
| 55 | 55 | for bucket_name in self.case_buckets_to_delete: | |
| 56 | - storage.Bucket(CLIENT, name=bucket_name).delete() | ||
| 56 | + CLIENT.bucket(bucket_name).delete() | ||
| 57 | 57 | ||
| 58 | 58 | def test_create_bucket(self): | |
| 59 | 59 | new_bucket_name = 'a-new-bucket' | |
@@ -114,7 +114,7 @@ def tearDown(self): | |||
| 114 | 114 | class TestStorageWriteFiles(TestStorageFiles): | |
| 115 | 115 | ||
| 116 | 116 | def test_large_file_write_from_stream(self): | |
| 117 | - blob = storage.Blob(bucket=self.bucket, name='LargeFile') | ||
| 117 | + blob = self.bucket.blob('LargeFile') | ||
| 118 | 118 | self.assertEqual(blob._properties, {}) | |
| 119 | 119 | ||
| 120 | 120 | file_data = self.FILES['big'] | |
@@ -128,7 +128,7 @@ def test_large_file_write_from_stream(self): | |||
| 128 | 128 | self.assertEqual(md5_hash, file_data['hash']) | |
| 129 | 129 | ||
| 130 | 130 | def test_small_file_write_from_filename(self): | |
| 131 | - blob = storage.Blob(bucket=self.bucket, name='SmallFile') | ||
| 131 | + blob = self.bucket.blob('SmallFile') | ||
| 132 | 132 | self.assertEqual(blob._properties, {}) | |
| 133 | 133 | ||
| 134 | 134 | file_data = self.FILES['simple'] | |
@@ -150,12 +150,12 @@ def test_write_metadata(self): | |||
| 150 | 150 | self.assertEqual(blob.content_type, 'image/png') | |
| 151 | 151 | ||
| 152 | 152 | def test_direct_write_and_read_into_file(self): | |
| 153 | - blob = storage.Blob(bucket=self.bucket, name='MyBuffer') | ||
| 153 | + blob = self.bucket.blob('MyBuffer') | ||
| 154 | 154 | file_contents = b'Hello World' | |
| 155 | 155 | blob.upload_from_string(file_contents) | |
| 156 | 156 | self.case_blobs_to_delete.append(blob) | |
| 157 | 157 | ||
| 158 | - same_blob = storage.Blob(bucket=self.bucket, name='MyBuffer') | ||
| 158 | + same_blob = self.bucket.blob('MyBuffer') | ||
| 159 | 159 | same_blob.reload() # Initialize properties. | |
| 160 | 160 | temp_filename = tempfile.mktemp() | |
| 161 | 161 | with open(temp_filename, 'wb') as file_obj: | |
@@ -309,7 +309,7 @@ def setUp(self): | |||
| 309 | 309 | with open(logo_path, 'rb') as file_obj: | |
| 310 | 310 | self.LOCAL_FILE = file_obj.read() | |
| 311 | 311 | ||
| 312 | - blob = storage.Blob(bucket=self.bucket, name='LogoToSign.jpg') | ||
| 312 | + blob = self.bucket.blob('LogoToSign.jpg') | ||
| 313 | 313 | blob.upload_from_string(self.LOCAL_FILE) | |
| 314 | 314 | self.case_blobs_to_delete.append(blob) | |
| 315 | 315 | ||
@@ -319,7 +319,7 @@ def tearDown(self): | |||
| 319 | 319 | blob.delete() | |
| 320 | 320 | ||
| 321 | 321 | def test_create_signed_read_url(self): | |
| 322 | - blob = storage.Blob(bucket=self.bucket, name='LogoToSign.jpg') | ||
| 322 | + blob = self.bucket.blob('LogoToSign.jpg') | ||
| 323 | 323 | expiration = int(time.time() + 5) | |
| 324 | 324 | signed_url = blob.generate_signed_url(expiration, method='GET', | |
| 325 | 325 | client=CLIENT) | |
@@ -329,7 +329,7 @@ def test_create_signed_read_url(self): | |||
| 329 | 329 | self.assertEqual(content, self.LOCAL_FILE) | |
| 330 | 330 | ||
| 331 | 331 | def test_create_signed_delete_url(self): | |
| 332 | - blob = storage.Blob(bucket=self.bucket, name='LogoToSign.jpg') | ||
| 332 | + blob = self.bucket.blob('LogoToSign.jpg') | ||
| 333 | 333 | expiration = int(time.time() + 283473274) | |
| 334 | 334 | signed_delete_url = blob.generate_signed_url(expiration, | |
| 335 | 335 | method='DELETE', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments