summaryrefslogtreecommitdiff
path: root/swiftclient
diff options
context:
space:
mode:
authorChristopher Bartz <bartz@dkrz.de>2016-12-08 13:42:35 +0100
committerChristopher Bartz <bartz@dkrz.de>2017-01-19 16:34:26 +0100
commit3934bd606acc2333ee9ae63a40baa35928ef908d (patch)
tree1181d58436365ce6d9863866c112f78e6677a081 /swiftclient
parentaea0585ddbc749b6f4d501430d41b671932c11a4 (diff)
downloadpython-swiftclient-3934bd606acc2333ee9ae63a40baa35928ef908d.tar.gz
prefix-based tempurls support
Implements client-side functionality for prefix-based tempurls. Please see: https://review.openstack.org/#/c/274048/ Change-Id: I8d7701daee888ed1120271a96c0660b01543ca2d
Diffstat (limited to 'swiftclient')
-rwxr-xr-xswiftclient/shell.py17
-rw-r--r--swiftclient/utils.py22
2 files changed, 28 insertions, 11 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py
index 9819bf3..6956a15 100755
--- a/swiftclient/shell.py
+++ b/swiftclient/shell.py
@@ -1222,9 +1222,8 @@ def st_auth(parser, args, thread_manager):
print('export OS_AUTH_TOKEN=%s' % sh_quote(token))
-st_tempurl_options = '''[--absolute]
- <method> <seconds> <path> <key>
-'''
+st_tempurl_options = '''[--absolute] [--prefix-based]
+ <method> <seconds> <path> <key>'''
st_tempurl_help = '''
@@ -1247,6 +1246,7 @@ Optional arguments:
--absolute Interpret the <seconds> positional argument as a Unix
timestamp rather than a number of seconds in the
future.
+ --prefix-based If present, a prefix-based tempURL will be generated.
'''.strip('\n')
@@ -1256,8 +1256,14 @@ def st_tempurl(parser, args, thread_manager):
dest='absolute_expiry', default=False,
help=("If present, seconds argument will be interpreted as a Unix "
"timestamp representing when the tempURL should expire, rather "
- "than an offset from the current time")
+ "than an offset from the current time"),
)
+ parser.add_argument(
+ '--prefix-based', action='store_true',
+ default=False,
+ help=("If present, a prefix-based tempURL will be generated."),
+ )
+
(options, args) = parse_args(parser, args)
args = args[1:]
if len(args) < 4:
@@ -1274,7 +1280,8 @@ def st_tempurl(parser, args, thread_manager):
method.upper())
try:
path = generate_temp_url(parsed.path, seconds, key, method,
- absolute=options['absolute_expiry'])
+ absolute=options['absolute_expiry'],
+ prefix=options['prefix_based'],)
except ValueError as err:
thread_manager.error(err)
return
diff --git a/swiftclient/utils.py b/swiftclient/utils.py
index e14602d..5ba6d5b 100644
--- a/swiftclient/utils.py
+++ b/swiftclient/utils.py
@@ -62,12 +62,14 @@ def prt_bytes(num_bytes, human_flag):
return '%.1f%s' % (num, suffix)
-def generate_temp_url(path, seconds, key, method, absolute=False):
+def generate_temp_url(path, seconds, key, method, absolute=False,
+ prefix=False):
"""Generates a temporary URL that gives unauthenticated access to the
Swift object.
- :param path: The full path to the Swift object. Example:
- /v1/AUTH_account/c/o.
+ :param path: The full path to the Swift object or prefix if
+ a prefix-based temporary URL should be generated. Example:
+ /v1/AUTH_account/c/o or /v1/AUTH_account/c/prefix.
:param seconds: If absolute is False then this specifies the amount of time
in seconds for which the temporary URL will be valid. If absolute is
True then this specifies an absolute time at which the temporary URL
@@ -80,6 +82,7 @@ def generate_temp_url(path, seconds, key, method, absolute=False):
:param absolute: if True then the seconds parameter is interpreted as an
absolute Unix time, otherwise seconds is interpreted as a relative time
offset from current time.
+ :param prefix: if True then a prefix-based temporary URL will be generated.
:raises: ValueError if seconds is not a whole number or path is not to
an object.
:return: the path portion of a temporary URL
@@ -103,8 +106,12 @@ def generate_temp_url(path, seconds, key, method, absolute=False):
path_for_body = path
parts = path_for_body.split('/', 4)
- if len(parts) != 5 or parts[0] or not all(parts[1:]):
- raise ValueError('path must be full path to an object e.g. /v1/a/c/o')
+ if len(parts) != 5 or parts[0] or not all(parts[1:(4 if prefix else 5)]):
+ if prefix:
+ raise ValueError('path must at least contain /v1/a/c/')
+ else:
+ raise ValueError('path must be full path to an object'
+ ' e.g. /v1/a/c/o')
standard_methods = ['GET', 'PUT', 'HEAD', 'POST', 'DELETE']
if method.upper() not in standard_methods:
@@ -116,7 +123,8 @@ def generate_temp_url(path, seconds, key, method, absolute=False):
expiration = int(time.time() + seconds)
else:
expiration = seconds
- hmac_body = u'\n'.join([method.upper(), str(expiration), path_for_body])
+ hmac_body = u'\n'.join([method.upper(), str(expiration),
+ ('prefix:' if prefix else '') + path_for_body])
# Encode to UTF-8 for py3 compatibility
if not isinstance(key, six.binary_type):
@@ -125,6 +133,8 @@ def generate_temp_url(path, seconds, key, method, absolute=False):
temp_url = u'{path}?temp_url_sig={sig}&temp_url_expires={exp}'.format(
path=path_for_body, sig=sig, exp=expiration)
+ if prefix:
+ temp_url += u'&temp_url_prefix={}'.format(parts[4])
# Have return type match path from caller
if isinstance(path, six.binary_type):
return temp_url.encode('utf-8')