forked from googleapis/google-cloud-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix googleapis#64 - Add Bucket.copy_key() and Key.rename() methods.
- Loading branch information
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -132,6 +132,19 @@ def exists(self): | |
|
||
return self.bucket.get_key(self.name) is not None | ||
|
||
def rename(self, new_name): | ||
"""Renames this key. | ||
:type new_name: string | ||
:param new_name: The new name for this key. | ||
""" | ||
new_key = self.bucket.copy_key(self, self.bucket, new_name) | ||
self.delete() | ||
# This feels like a dirty hack, but since the bucket is the same we can | ||
# just change the name attribute of this instance to have it point to the | ||
# new key. | ||
self.name = new_key.name | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gsalgado
via email
Author
Owner
|
||
|
||
def delete(self): | ||
"""Deletes a key from Cloud Storage. | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import unittest2 | ||
|
||
from gcloud.storage.bucket import Bucket | ||
from gcloud.storage.connection import Connection | ||
from gcloud.storage.key import Key | ||
|
||
|
||
class TestKey(unittest2.TestCase): | ||
|
||
def setUp(self): | ||
# Mock Connection.api_request with a method that just stores the HTTP | ||
# method, path and query params in an instance variable for later | ||
# inspection. | ||
# TODO: It'd be better to make the Connection talk to a local HTTP server | ||
# that we can inspect, but a simple test using a mock is certainly better | ||
# than no tests. | ||
self.connection = Connection('project-name') | ||
self.connection.api_request = self.mock_api_request | ||
self.api_request_calls = [] | ||
|
||
def mock_api_request(self, method, path=None, query_params=None, | ||
data=None, content_type=None, | ||
api_base_url=None, api_version=None, | ||
expect_json=True): | ||
self.api_request_calls.append([method, path, query_params]) | ||
|
||
def test_rename(self): | ||
bucket = Bucket(self.connection, 'bucket') | ||
key = Key(bucket, 'key') | ||
orig_key_path = key.path | ||
key.rename('new-name') | ||
expected = [ | ||
['POST', orig_key_path + '/copyTo/b/bucket/o/new-name', None], | ||
['DELETE', orig_key_path, None]] | ||
self.assertEqual(key.name, 'new-name') | ||
self.assertEqual(self.api_request_calls, expected) |
How about returning the new key as an alternative to modifying the state of this one?