We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents 81a2b9a + 78312ca commit d0ecb89Copy full SHA for d0ecb89
6 files changed
doc/source/command-objects/object.rst
@@ -13,9 +13,14 @@ Upload object to container
13
.. code:: bash
14
15
os object create
16
+ [--name <name>]
17
<container>
18
<filename> [<filename> ...]
19
20
+.. option:: --name <name>
21
+
22
+ Upload a file and rename it. Can only be used when uploading a single object
23
24
.. describe:: <container>
25
26
Container for new object
openstackclient/api/object_store_v1.py
@@ -214,13 +214,16 @@ def object_create(
214
self,
215
container=None,
216
object=None,
217
+ name=None,
218
):
219
"""Create an object inside a container
220
221
:param string container:
222
name of container to store object
223
:param string object:
224
local path to object
225
+ :param string name:
226
+ name of object to create
227
:returns:
228
dict of returned headers
229
"""
@@ -229,8 +232,12 @@ def object_create(
232
# TODO(dtroyer): What exception to raise here?
230
233
return {}
231
234
235
+ # For uploading a file, if name is provided then set it as the
236
+ # object's name in the container.
237
+ object_name_str = name if name else object
238
239
full_url = "%s/%s" % (urllib.parse.quote(container),
- urllib.parse.quote(object))
240
+ urllib.parse.quote(object_name_str))
241
with io.open(object, 'rb') as f:
242
response = self.create(
243
full_url,
@@ -240,7 +247,7 @@ def object_create(
247
data = {
248
'account': self._find_account_id(),
249
'container': container,
- 'object': object,
250
+ 'object': object_name_str,
244
251
'x-trans-id': response.headers.get('X-Trans-Id'),
245
252
'etag': response.headers.get('Etag'),
246
253
}
openstackclient/object/v1/object.py
@@ -19,6 +19,7 @@
from osc_lib.cli import parseractions
from osc_lib.command import command
+from osc_lib import exceptions
from osc_lib import utils
import six
@@ -44,10 +45,20 @@ def get_parser(self, prog_name):
44
45
nargs="+",
46
help='Local filename(s) to upload',
47
)
48
+ parser.add_argument(
49
+ '--name',
50
+ metavar='<name>',
51
+ help='Upload a file and rename it. '
52
+ 'Can only be used when uploading a single object'
53
+ )
54
return parser
55
56
def take_action(self, parsed_args):
-
57
+ if parsed_args.name:
58
+ if len(parsed_args.objects) > 1:
59
+ msg = _('Attempting to upload multiple objects and '
60
+ 'using --name is not permitted')
61
+ raise exceptions.CommandError(msg)
62
results = []
63
for obj in parsed_args.objects:
64
if len(obj) > 1024:
@@ -57,6 +68,7 @@ def take_action(self, parsed_args):
68
data = self.app.client_manager.object_store.object_create(
69
container=parsed_args.container,
70
object=obj,
71
+ name=parsed_args.name,
72
73
results.append(data)
74
openstackclient/tests/unit/object/v1/fakes.py
@@ -75,6 +75,8 @@
75
'last_modified': object_modified_2,
76
77
78
+object_upload_name = 'test-object-name'
79
80
81
class TestObjectv1(utils.TestCommand):
82
openstackclient/tests/unit/object/v1/test_object_all.py
@@ -13,6 +13,7 @@
import copy
from requests_mock.contrib import fixture
from openstackclient.object.v1 import object as object_cmds
@@ -35,6 +36,27 @@ def setUp(self):
35
36
# Get the command object to test
37
self.cmd = object_cmds.CreateObject(self.app, None)
38
39
+ def test_multiple_object_create_with_object_name(self):
40
+ arglist = [
41
+ object_fakes.container_name,
42
+ object_fakes.object_name_1,
43
+ object_fakes.object_name_2,
+ '--name', object_fakes.object_upload_name,
+ ]
+ verifylist = [
+ ('container', object_fakes.container_name),
+ ('objects', [object_fakes.object_name_1,
+ object_fakes.object_name_2]),
+ ('name', object_fakes.object_upload_name),
+ parsed_args = self.check_parser(self.cmd, arglist, verifylist)
+ self.assertRaises(exceptions.CommandError,
+ self.cmd.take_action,
+ parsed_args)
class TestObjectList(TestObjectAll):
releasenotes/notes/bug-1607972-a910a9fbdb81da57.yaml
@@ -0,0 +1,5 @@
1
+---
2
+features:
3
+ - Add ``--name`` option to command ``object create``
4
+ for uploading a file and renaming it.
5
+ [Bug `1607972 <https://bugs.launchpad.net/bugs/1607972>`_]
0 commit comments