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)
def get_key(self, key): """Get a key object by name. This will return None if the key doesn't exist:: >>> from gcloud import storage >>> connection = storage.get_connection(project, email, key_path) >>> bucket = connection.get_bucket('my-bucket') >>> print bucket.get_key('/path/to/key.txt') <Key: my-bucket, /path/to/key.txt> >>> print bucket.get_key('/does-not-exist.txt') None :type key: string or :class:`gcloud.storage.key.Key` :param key: The name of the key to retrieve. :rtype: :class:`gcloud.storage.key.Key` or None :returns: The key object if it exists, otherwise None. """ # Coerce this to a key object (either from a Key or a string). key = self.new_key(key) try: response = self.connection.api_request(method='GET', path=key.path) return Key.from_dict(response, bucket=self) except exceptions.NotFoundError: return None
def new_key(self, key): """Given a path name (or a Key), return a :class:`gcloud.storage.key.Key` object. This is really useful when you're not sure if you have a Key object or a string path name. Given either of those types, this returns the corresponding Key object. :type key: string or :class:`gcloud.storage.key.Key` :param key: A path name or actual key object. :rtype: :class:`gcloud.storage.key.Key` :returns: A Key object with the path provided. """ if isinstance(key, Key): return key # Support Python 2 and 3. try: string_type = basestring except NameError: string_type = str if isinstance(key, string_type): return Key(bucket=self, name=key) raise TypeError('Invalid key: %s' % key)
def get_key(self, key): """Get a key object by name. This will return None if the key doesn't exist:: >>> from gcloud import storage >>> connection = storage.get_connection(project, email, key_path) >>> bucket = connection.get_bucket('my-bucket') >>> print bucket.get_key('/path/to/key.txt') <Key: my-bucket, /path/to/key.txt> >>> print bucket.get_key('/does-not-exist.txt') None :type key: string or :class:`gcloud.storage.key.Key` :param key: The name of the key to retrieve. :rtype: :class:`gcloud.storage.key.Key` or None :returns: The key object if it exists, otherwise None. """ # Coerce this to a key object (either from a Key or a string). key = self.new_key(key) try: response = self.connection.api_request(method='GET', path=key.path) return Key.from_dict(response, bucket=self) except exceptions.NotFound: return None
def get_items_from_response(self, response): """Yield :class:`.storage.key.Key` items from response. :type response: dict :param response: The JSON API response for a page of keys. """ self.prefixes = tuple(response.get('prefixes', ())) for item in response.get('items', []): yield Key.from_dict(item, bucket=self.bucket)
def get_items_from_response(self, response): """Factory method which yields :class:`gcloud.storage.key.Key` items from a response. :type response: dict :param response: The JSON API response for a page of keys. """ from gcloud.storage.key import Key for item in response.get('items', []): yield Key.from_dict(item, bucket=self.bucket)
def get_items_from_response(self, response): """Factory method, yields :class:`.storage.key.Key` items from response. :type response: dict :param response: The JSON API response for a page of keys. """ from gcloud.storage.key import Key for item in response.get('items', []): yield Key.from_dict(item, bucket=self.bucket)
def test_new_key_existing(self): from gcloud.storage.key import Key existing = Key() bucket = self._makeOne() self.assertTrue(bucket.new_key(existing) is existing)