Example #1
0
    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)
Example #2
0
 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)
Example #3
0
 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)