| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -428,7 +428,9 @@ def delete_blobs(self, blobs, on_error=None, connection=None): | |||
| 428 | 428 | else: | |
| 429 | 429 | raise | |
| 430 | 430 | ||
| 431 | - def copy_blob(self, blob, destination_bucket, new_name=None): | ||
| 431 | + @staticmethod | ||
| 432 | + def copy_blob(blob, destination_bucket, new_name=None, | ||
| 433 | + connection=None): | ||
| 432 | 434 | """Copy the given blob to the given bucket, optionally with a new name. | |
| 433 | 435 | ||
| 434 | 436 | :type blob: string or :class:`gcloud.storage.blob.Blob` | |
@@ -441,18 +443,24 @@ def copy_blob(self, blob, destination_bucket, new_name=None): | |||
| 441 | 443 | :type new_name: string | |
| 442 | 444 | :param new_name: (optional) the new name for the copied file. | |
| 443 | 445 | ||
| 446 | + :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| 447 | + ``NoneType`` | ||
| 448 | + :param connection: Optional. The connection to use when sending | ||
| 449 | + requests. If not provided, falls back to default. | ||
| 450 | + | ||
| 444 | 451 | :rtype: :class:`gcloud.storage.blob.Blob` | |
| 445 | 452 | :returns: The new Blob. | |
| 446 | 453 | """ | |
| 454 | + connection = _require_connection(connection) | ||
| 447 | 455 | if new_name is None: | |
| 448 | 456 | new_name = blob.name | |
| 449 | 457 | new_blob = Blob(bucket=destination_bucket, name=new_name) | |
| 450 | 458 | api_path = blob.path + '/copyTo' + new_blob.path | |
| 451 | - copy_result = self.connection.api_request(method='POST', path=api_path) | ||
| 459 | + copy_result = connection.api_request(method='POST', path=api_path) | ||
| 452 | 460 | new_blob._set_properties(copy_result) | |
| 453 | 461 | return new_blob | |
| 454 | 462 | ||
| 455 | - def upload_file(self, filename, blob_name=None): | ||
| 463 | + def upload_file(self, filename, blob_name=None, connection=None): | ||
| 456 | 464 | """Shortcut method to upload a file into this bucket. | |
| 457 | 465 | ||
| 458 | 466 | Use this method to quickly put a local file in Cloud Storage. | |
@@ -485,16 +493,21 @@ def upload_file(self, filename, blob_name=None): | |||
| 485 | 493 | of the bucket with the same name as on your local | |
| 486 | 494 | file system. | |
| 487 | 495 | ||
| 496 | + :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| 497 | + ``NoneType`` | ||
| 498 | + :param connection: Optional. The connection to use when sending | ||
| 499 | + requests. If not provided, falls back to default. | ||
| 500 | + | ||
| 488 | 501 | :rtype: :class:`Blob` | |
| 489 | 502 | :returns: The updated Blob object. | |
| 490 | 503 | """ | |
| 491 | 504 | if blob_name is None: | |
| 492 | 505 | blob_name = os.path.basename(filename) | |
| 493 | 506 | blob = Blob(bucket=self, name=blob_name) | |
| 494 | - blob.upload_from_filename(filename) | ||
| 507 | + blob.upload_from_filename(filename, connection=connection) | ||
| 495 | 508 | return blob | |
| 496 | 509 | ||
| 497 | - def upload_file_object(self, file_obj, blob_name=None): | ||
| 510 | + def upload_file_object(self, file_obj, blob_name=None, connection=None): | ||
| 498 | 511 | """Shortcut method to upload a file object into this bucket. | |
| 499 | 512 | ||
| 500 | 513 | Use this method to quickly put a local file in Cloud Storage. | |
@@ -527,13 +540,18 @@ def upload_file_object(self, file_obj, blob_name=None): | |||
| 527 | 540 | of the bucket with the same name as on your local | |
| 528 | 541 | file system. | |
| 529 | 542 | ||
| 543 | + :type connection: :class:`gcloud.storage.connection.Connection` or | ||
| 544 | + ``NoneType`` | ||
| 545 | + :param connection: Optional. The connection to use when sending | ||
| 546 | + requests. If not provided, falls back to default. | ||
| 547 | + | ||
| 530 | 548 | :rtype: :class:`Blob` | |
| 531 | 549 | :returns: The updated Blob object. | |
| 532 | 550 | """ | |
| 533 | 551 | if blob_name is None: | |
| 534 | 552 | blob_name = os.path.basename(file_obj.name) | |
| 535 | 553 | blob = Blob(bucket=self, name=blob_name) | |
| 536 | - blob.upload_from_file(file_obj) | ||
| 554 | + blob.upload_from_file(file_obj, connection=connection) | ||
| 537 | 555 | return blob | |
| 538 | 556 | ||
| 539 | 557 | @property | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -194,8 +194,10 @@ def test_create_no_project(self): | |||
| 194 | 194 | from gcloud._testing import _monkey_defaults | |
| 195 | 195 | BUCKET_NAME = 'bucket-name' | |
| 196 | 196 | bucket = self._makeOne(BUCKET_NAME) | |
| 197 | + CONNECTION = object() | ||
| 197 | 198 | with _monkey_defaults(project=None): | |
| 198 | - self.assertRaises(EnvironmentError, bucket.create) | ||
| 199 | + self.assertRaises(EnvironmentError, bucket.create, | ||
| 200 | + connection=CONNECTION) | ||
| 199 | 201 | ||
| 200 | 202 | def test_create_hit_explicit_project(self): | |
| 201 | 203 | BUCKET_NAME = 'bucket-name' | |
@@ -391,7 +393,8 @@ def test_delete_explicit_too_many(self): | |||
| 391 | 393 | ||
| 392 | 394 | # Make the Bucket refuse to delete with 2 objects. | |
| 393 | 395 | bucket._MAX_OBJECTS_FOR_BUCKET_DELETE = 1 | |
| 394 | - self.assertRaises(ValueError, bucket.delete, force=True) | ||
| 396 | + self.assertRaises(ValueError, bucket.delete, force=True, | ||
| 397 | + connection=connection) | ||
| 395 | 398 | self.assertEqual(connection._deleted_buckets, []) | |
| 396 | 399 | ||
| 397 | 400 | def test_delete_blob_miss(self): | |
@@ -420,8 +423,8 @@ def test_delete_blob_hit(self): | |||
| 420 | 423 | def test_delete_blobs_empty(self): | |
| 421 | 424 | NAME = 'name' | |
| 422 | 425 | connection = _Connection() | |
| 423 | - bucket = self._makeOne(NAME, connection) | ||
| 424 | - bucket.delete_blobs([]) | ||
| 426 | + bucket = self._makeOne(NAME) | ||
| 427 | + bucket.delete_blobs([], connection=connection) | ||
| 425 | 428 | self.assertEqual(connection._requested, []) | |
| 426 | 429 | ||
| 427 | 430 | def test_delete_blobs_hit(self): | |
@@ -478,10 +481,10 @@ class _Blob(object): | |||
| 478 | 481 | path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME) | |
| 479 | 482 | ||
| 480 | 483 | connection = _Connection({}) | |
| 481 | - source = self._makeOne(SOURCE, connection) | ||
| 482 | - dest = self._makeOne(DEST, connection) | ||
| 484 | + source = self._makeOne(SOURCE) | ||
| 485 | + dest = self._makeOne(DEST) | ||
| 483 | 486 | blob = _Blob() | |
| 484 | - new_blob = source.copy_blob(blob, dest) | ||
| 487 | + new_blob = source.copy_blob(blob, dest, connection=connection) | ||
| 485 | 488 | self.assertTrue(new_blob.bucket is dest) | |
| 486 | 489 | self.assertEqual(new_blob.name, BLOB_NAME) | |
| 487 | 490 | kw, = connection._requested | |
@@ -501,10 +504,11 @@ class _Blob(object): | |||
| 501 | 504 | path = '/b/%s/o/%s' % (SOURCE, BLOB_NAME) | |
| 502 | 505 | ||
| 503 | 506 | connection = _Connection({}) | |
| 504 | - source = self._makeOne(SOURCE, connection) | ||
| 505 | - dest = self._makeOne(DEST, connection) | ||
| 507 | + source = self._makeOne(SOURCE) | ||
| 508 | + dest = self._makeOne(DEST) | ||
| 506 | 509 | blob = _Blob() | |
| 507 | - new_blob = source.copy_blob(blob, dest, NEW_NAME) | ||
| 510 | + new_blob = source.copy_blob(blob, dest, NEW_NAME, | ||
| 511 | + connection=connection) | ||
| 508 | 512 | self.assertTrue(new_blob.bucket is dest) | |
| 509 | 513 | self.assertEqual(new_blob.name, NEW_NAME) | |
| 510 | 514 | kw, = connection._requested | |
@@ -526,13 +530,14 @@ def __init__(self, bucket, name): | |||
| 526 | 530 | self._bucket = bucket | |
| 527 | 531 | self._name = name | |
| 528 | 532 | ||
| 529 | - def upload_from_filename(self, filename): | ||
| 530 | - _uploaded.append((self._bucket, self._name, filename)) | ||
| 533 | + def upload_from_filename(self, filename, connection=None): | ||
| 534 | + _uploaded.append((self._bucket, self._name, filename, | ||
| 535 | + connection)) | ||
| 531 | 536 | ||
| 532 | 537 | bucket = self._makeOne() | |
| 533 | 538 | with _Monkey(MUT, Blob=_Blob): | |
| 534 | 539 | bucket.upload_file(FILENAME) | |
| 535 | - self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME)]) | ||
| 540 | + self.assertEqual(_uploaded, [(bucket, BASENAME, FILENAME, None)]) | ||
| 536 | 541 | ||
| 537 | 542 | def test_upload_file_explicit_blob(self): | |
| 538 | 543 | from gcloud._testing import _Monkey | |
@@ -547,13 +552,14 @@ def __init__(self, bucket, name): | |||
| 547 | 552 | self._bucket = bucket | |
| 548 | 553 | self._name = name | |
| 549 | 554 | ||
| 550 | - def upload_from_filename(self, filename): | ||
| 551 | - _uploaded.append((self._bucket, self._name, filename)) | ||
| 555 | + def upload_from_filename(self, filename, connection=None): | ||
| 556 | + _uploaded.append((self._bucket, self._name, filename, | ||
| 557 | + connection)) | ||
| 552 | 558 | ||
| 553 | 559 | bucket = self._makeOne() | |
| 554 | 560 | with _Monkey(MUT, Blob=_Blob): | |
| 555 | 561 | bucket.upload_file(FILENAME, BLOB_NAME) | |
| 556 | - self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILENAME)]) | ||
| 562 | + self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILENAME, None)]) | ||
| 557 | 563 | ||
| 558 | 564 | def test_upload_file_object_no_blob(self): | |
| 559 | 565 | from gcloud._testing import _Monkey | |
@@ -568,13 +574,13 @@ def __init__(self, bucket, name): | |||
| 568 | 574 | self._bucket = bucket | |
| 569 | 575 | self._name = name | |
| 570 | 576 | ||
| 571 | - def upload_from_file(self, fh): | ||
| 572 | - _uploaded.append((self._bucket, self._name, fh)) | ||
| 577 | + def upload_from_file(self, fh, connection=None): | ||
| 578 | + _uploaded.append((self._bucket, self._name, fh, connection)) | ||
| 573 | 579 | ||
| 574 | 580 | bucket = self._makeOne() | |
| 575 | 581 | with _Monkey(MUT, Blob=_Blob): | |
| 576 | 582 | found = bucket.upload_file_object(FILEOBJECT) | |
| 577 | - self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT)]) | ||
| 583 | + self.assertEqual(_uploaded, [(bucket, FILENAME, FILEOBJECT, None)]) | ||
| 578 | 584 | self.assertTrue(isinstance(found, _Blob)) | |
| 579 | 585 | self.assertEqual(found._name, FILENAME) | |
| 580 | 586 | self.assertTrue(found._bucket is bucket) | |
@@ -593,13 +599,13 @@ def __init__(self, bucket, name): | |||
| 593 | 599 | self._bucket = bucket | |
| 594 | 600 | self._name = name | |
| 595 | 601 | ||
| 596 | - def upload_from_file(self, fh): | ||
| 597 | - _uploaded.append((self._bucket, self._name, fh)) | ||
| 602 | + def upload_from_file(self, fh, connection=None): | ||
| 603 | + _uploaded.append((self._bucket, self._name, fh, connection)) | ||
| 598 | 604 | ||
| 599 | 605 | bucket = self._makeOne() | |
| 600 | 606 | with _Monkey(MUT, Blob=_Blob): | |
| 601 | 607 | found = bucket.upload_file_object(FILEOBJECT, BLOB_NAME) | |
| 602 | - self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT)]) | ||
| 608 | + self.assertEqual(_uploaded, [(bucket, BLOB_NAME, FILEOBJECT, None)]) | ||
| 603 | 609 | self.assertTrue(isinstance(found, _Blob)) | |
| 604 | 610 | self.assertEqual(found._name, BLOB_NAME) | |
| 605 | 611 | self.assertTrue(found._bucket is bucket) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments