Add commands for object upload and delete
Add commands to upload an object to a container, and to delete an object from a container. Change-Id: I37c02315495bba5abe612733d1109a3d4ce256a1 implements: bp swift-client
This commit is contained in:
parent
fed6921336
commit
8af26a51c3
@ -24,6 +24,52 @@ except ImportError:
|
||||
from urlparse import urlparse # noqa
|
||||
|
||||
|
||||
def create_object(
|
||||
api,
|
||||
url,
|
||||
container,
|
||||
object,
|
||||
):
|
||||
"""Create an object, upload it to a container
|
||||
|
||||
:param api: a restapi object
|
||||
:param url: endpoint
|
||||
:param container: name of container to store object
|
||||
:param object: local path to object
|
||||
:returns: dict of returned headers
|
||||
"""
|
||||
|
||||
full_url = "%s/%s/%s" % (url, container, object)
|
||||
response = api.put(full_url, data=open(object))
|
||||
url_parts = urlparse(url)
|
||||
data = {
|
||||
'account': url_parts.path.split('/')[-1],
|
||||
'container': container,
|
||||
'object': object,
|
||||
}
|
||||
data['x-trans-id'] = response.headers.get('X-Trans-Id', None)
|
||||
data['etag'] = response.headers.get('Etag', None)
|
||||
|
||||
return data
|
||||
|
||||
|
||||
def delete_object(
|
||||
api,
|
||||
url,
|
||||
container,
|
||||
object,
|
||||
):
|
||||
"""Delete an object stored in a container
|
||||
|
||||
:param api: a restapi object
|
||||
:param url: endpoint
|
||||
:param container: name of container that stores object
|
||||
:param container: name of object to delete
|
||||
"""
|
||||
|
||||
api.delete("%s/%s/%s" % (url, container, object))
|
||||
|
||||
|
||||
def list_objects(
|
||||
api,
|
||||
url,
|
||||
|
@ -19,6 +19,7 @@
|
||||
import logging
|
||||
import six
|
||||
|
||||
from cliff import command
|
||||
from cliff import lister
|
||||
from cliff import show
|
||||
|
||||
@ -26,6 +27,68 @@ from openstackclient.common import utils
|
||||
from openstackclient.object.v1.lib import object as lib_object
|
||||
|
||||
|
||||
class CreateObject(show.ShowOne):
|
||||
"""Upload an object to a container"""
|
||||
|
||||
log = logging.getLogger(__name__ + '.CreateObject')
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(CreateObject, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'container',
|
||||
metavar='<container>',
|
||||
help='Container to store new object',
|
||||
)
|
||||
parser.add_argument(
|
||||
'object',
|
||||
metavar='<object-name>',
|
||||
help='Local path of object to upload',
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug('take_action(%s)', parsed_args)
|
||||
|
||||
data = lib_object.create_object(
|
||||
self.app.restapi,
|
||||
self.app.client_manager.object_store.endpoint,
|
||||
parsed_args.container,
|
||||
parsed_args.object,
|
||||
)
|
||||
|
||||
return zip(*sorted(six.iteritems(data)))
|
||||
|
||||
|
||||
class DeleteObject(command.Command):
|
||||
"""Delete an object within a container"""
|
||||
|
||||
log = logging.getLogger(__name__ + '.DeleteObject')
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(DeleteObject, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'container',
|
||||
metavar='<container>',
|
||||
help='Container that stores the object to delete',
|
||||
)
|
||||
parser.add_argument(
|
||||
'object',
|
||||
metavar='<object-name>',
|
||||
help='Object to delete',
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug('take_action(%s)', parsed_args)
|
||||
|
||||
lib_object.delete_object(
|
||||
self.app.restapi,
|
||||
self.app.client_manager.object_store.endpoint,
|
||||
parsed_args.container,
|
||||
parsed_args.object,
|
||||
)
|
||||
|
||||
|
||||
class ListObject(lister.Lister):
|
||||
"""List objects"""
|
||||
|
||||
|
@ -272,6 +272,8 @@ openstack.object_store.v1 =
|
||||
container_delete = openstackclient.object.v1.container:DeleteContainer
|
||||
container_list = openstackclient.object.v1.container:ListContainer
|
||||
container_show = openstackclient.object.v1.container:ShowContainer
|
||||
object_create = openstackclient.object.v1.object:CreateObject
|
||||
object_delete = openstackclient.object.v1.object:DeleteObject
|
||||
object_list = openstackclient.object.v1.object:ListObject
|
||||
object_show = openstackclient.object.v1.object:ShowObject
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user