summaryrefslogtreecommitdiff
path: root/swiftclient
diff options
context:
space:
mode:
Diffstat (limited to 'swiftclient')
-rw-r--r--swiftclient/service.py13
-rwxr-xr-xswiftclient/shell.py88
2 files changed, 90 insertions, 11 deletions
diff --git a/swiftclient/service.py b/swiftclient/service.py
index d7c05ca..7c55769 100644
--- a/swiftclient/service.py
+++ b/swiftclient/service.py
@@ -12,6 +12,7 @@
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
+import os
from concurrent.futures import as_completed, CancelledError, TimeoutError
from copy import deepcopy
from errno import EEXIST, ENOENT
@@ -162,6 +163,8 @@ _default_local_options = {
'read_acl': None,
'write_acl': None,
'out_file': None,
+ 'out_directory': None,
+ 'remove_prefix': False,
'no_download': False,
'long': False,
'totals': False,
@@ -889,7 +892,9 @@ class SwiftService(object):
'no_download': False,
'header': [],
'skip_identical': False,
- 'out_file': None
+ 'out_directory': None,
+ 'out_file': None,
+ 'remove_prefix': False,
}
:returns: A generator for returning the results of the download
@@ -986,6 +991,12 @@ class SwiftService(object):
options['skip_identical'] = (options['skip_identical'] and
out_file != '-')
+ if options['prefix'] and options['remove_prefix']:
+ path = path[len(options['prefix']):].lstrip('/')
+
+ if options['out_directory']:
+ path = os.path.join(options['out_directory'], path)
+
if options['skip_identical']:
filename = out_file if out_file else path
try:
diff --git a/swiftclient/shell.py b/swiftclient/shell.py
index 0663e4f..a77ea07 100755
--- a/swiftclient/shell.py
+++ b/swiftclient/shell.py
@@ -32,14 +32,19 @@ from swiftclient.utils import config_true_value, generate_temp_url, prt_bytes
from swiftclient.multithreading import OutputManager
from swiftclient.exceptions import ClientException
from swiftclient import __version__ as client_version
-from swiftclient.service import SwiftService, SwiftError, SwiftUploadObject
+from swiftclient.service import SwiftService, SwiftError, \
+ SwiftUploadObject, get_conn
from swiftclient.command_helpers import print_account_stats, \
print_container_stats, print_object_stats
+try:
+ from shlex import quote as sh_quote
+except ImportError:
+ from pipes import quote as sh_quote
BASENAME = 'swift'
-commands = ('delete', 'download', 'list', 'post',
- 'stat', 'upload', 'capabilities', 'info', 'tempurl')
+commands = ('delete', 'download', 'list', 'post', 'stat', 'upload',
+ 'capabilities', 'info', 'tempurl', 'auth')
def immediate_exit(signum, frame):
@@ -147,9 +152,11 @@ def st_delete(parser, args, output_manager):
st_download_options = '''[--all] [--marker] [--prefix <prefix>]
- [--output <out_file>] [--object-threads <threads>]
+ [--output <out_file>] [--output-dir <out_directory>]
+ [--object-threads <threads>]
[--container-threads <threads>] [--no-download]
- [--skip-identical] <container> <object>
+ [--skip-identical] [--remove-prefix]
+ <container> <object>
'''
st_download_help = '''
@@ -168,9 +175,15 @@ Optional arguments:
--marker Marker to use when starting a container or account
download.
--prefix <prefix> Only download items beginning with <prefix>
+ --remove-prefix An optional flag for --prefix <prefix>, use this
+ option to download items without <prefix>
--output <out_file> For a single file download, stream the output to
<out_file>. Specifying "-" as <out_file> will
redirect to stdout.
+ --output-dir <out_directory>
+ An optional directory to which to store objects.
+ By default, all objects are recreated in the current
+ directory.
--object-threads <threads>
Number of threads to use for downloading objects.
Default is 10.
@@ -205,6 +218,14 @@ def st_download(parser, args, output_manager):
'download, stream the output to <out_file>. '
'Specifying "-" as <out_file> will redirect to stdout.')
parser.add_option(
+ '-D', '--output-dir', dest='out_directory',
+ help='An optional directory to which to store objects. '
+ 'By default, all objects are recreated in the current directory.')
+ parser.add_option(
+ '-r', '--remove-prefix', action='store_true', dest='remove_prefix',
+ default=False, help='An optional flag for --prefix <prefix>, '
+ 'use this option to download items without <prefix>.')
+ parser.add_option(
'', '--object-threads', type=int,
default=10, help='Number of threads to use for downloading objects. '
'Default is 10.')
@@ -234,6 +255,12 @@ def st_download(parser, args, output_manager):
if options.out_file and len(args) != 2:
exit('-o option only allowed for single file downloads')
+ if not options.prefix:
+ options.remove_prefix = False
+
+ if options.out_directory and len(args) == 2:
+ exit('Please use -o option for single file downloads and renames')
+
if (not args and not options.yes_all) or (args and options.yes_all):
output_manager.error('Usage: %s download %s\n%s', BASENAME,
st_download_options, st_download_help)
@@ -909,6 +936,46 @@ def st_capabilities(parser, args, output_manager):
st_info = st_capabilities
+st_auth_help = '''
+Display auth related authentication variables in shell friendly format.
+
+ Commands to run to export storage url and auth token into
+ OS_STORAGE_URL and OS_AUTH_TOKEN:
+
+ swift auth
+
+ Commands to append to a runcom file (e.g. ~/.bashrc, /etc/profile) for
+ automatic authentication:
+
+ swift auth -v -U test:tester -K testing \
+ -A http://localhost:8080/auth/v1.0
+
+'''.strip('\n')
+
+
+def st_auth(parser, args, thread_manager):
+ (options, args) = parse_args(parser, args)
+ _opts = vars(options)
+ if options.verbose > 1:
+ if options.auth_version in ('1', '1.0'):
+ print('export ST_AUTH=%s' % sh_quote(options.auth))
+ print('export ST_USER=%s' % sh_quote(options.user))
+ print('export ST_KEY=%s' % sh_quote(options.key))
+ else:
+ print('export OS_IDENTITY_API_VERSION=%s' % sh_quote(
+ options.auth_version))
+ print('export OS_AUTH_VERSION=%s' % sh_quote(options.auth_version))
+ print('export OS_AUTH_URL=%s' % sh_quote(options.auth))
+ for k, v in sorted(_opts.items()):
+ if v and k.startswith('os_') and \
+ k not in ('os_auth_url', 'os_options'):
+ print('export %s=%s' % (k.upper(), sh_quote(v)))
+ else:
+ conn = get_conn(_opts)
+ url, token = conn.get_auth()
+ print('export OS_STORAGE_URL=%s' % sh_quote(url))
+ print('export OS_AUTH_TOKEN=%s' % sh_quote(token))
+
st_tempurl_options = '<method> <seconds> <path> <key>'
@@ -917,13 +984,13 @@ st_tempurl_help = '''
Generates a temporary URL for a Swift object.
Positional arguments:
- [method] An HTTP method to allow for this temporary URL.
+ <method> An HTTP method to allow for this temporary URL.
Usually 'GET' or 'PUT'.
- [seconds] The amount of time in seconds the temporary URL will
+ <seconds> The amount of time in seconds the temporary URL will
be valid for.
- [path] The full path to the Swift object. Example:
+ <path> The full path to the Swift object. Example:
/v1/AUTH_account/c/o.
- [key] The secret temporary URL key set on the Swift cluster.
+ <key> The secret temporary URL key set on the Swift cluster.
To set a key, run \'swift post -m
"Temp-URL-Key:b3968d0207b54ece87cccc06515a89d4"\'
'''.strip('\n')
@@ -1079,7 +1146,8 @@ Positional arguments:
or object.
upload Uploads files or directories to the given container.
capabilities List cluster capabilities.
- tempurl Create a temporary URL
+ tempurl Create a temporary URL.
+ auth Display auth related environment variables.
Examples:
%%prog download --help