diff --git a/linodecli/plugins/obj.py b/linodecli/plugins/obj.py index 921c93c9c..1d1c7e0da 100644 --- a/linodecli/plugins/obj.py +++ b/linodecli/plugins/obj.py @@ -3,6 +3,7 @@ import base64 from datetime import datetime import getpass +import glob import math import socket import sys @@ -180,39 +181,62 @@ def delete_bucket(get_client, args): sys.exit(0) +def _collect_files(file_path): + """ + Finds all files in a directory, recursively, and returns them as a list + """ + files = glob.glob(file_path+'/**', recursive=True) + ret = [] + + for f in files: + if os.path.isfile(f): + ret += [f] + + return ret + + +def _trim_prefix(filename, prefix): + """ + Removes a prefix from a filename; usually this is the first path segment + """ + return os.path.relpath(filename, prefix) + + def upload_object(get_client, args): """ Uploads an object to object storage """ parser = argparse.ArgumentParser(PLUGIN_BASE+' put') - parser.add_argument('file', metavar='FILE', type=str, nargs='+', - help="The files to upload.") + parser.add_argument('file', metavar='FILES_FOLDERS', type=str, nargs='+', + help="The files or folders to upload.") parser.add_argument('bucket', metavar='BUCKET', type=str, help="The bucket to put a file in.") parser.add_argument('--acl-public', action='store_true', help="If set, the new object can be downloaded without " "authentication.") - #parser.add_argument('--recursive', action='store_true', - # help="If set, upload directories recursively.") + parser.add_argument('--recursive', action='store_true', + help="If set, upload directories recursively.") parsed = parser.parse_args(args) client = get_client() - + file_list = [] # [filename, filepath, filesize] to_upload = [] to_multipart_upload = [] for c in parsed.file: # find the object file_path = os.path.expanduser(c) - if not os.path.isfile(file_path): - print('No file {}'.format(file_path)) - sys.exit(5) - - filename = os.path.split(file_path)[-1] - - file_size = os.path.getsize(file_path) + if os.path.isdir(file_path): + if parsed.recursive: + files = _collect_files(file_path) + else: + files = [file_path + f for f in os.listdir(file_path) if os.path.isfile(file_path + f)] + file_list.extend([[item, _trim_prefix(item, file_path), os.path.getsize(item)] for item in files]) + elif os.path.isfile(file_path): + file_list.append([file_path, os.path.basename(file_path), os.path.getsize(file_path)]) + for file_path, filename, file_size in file_list: if file_size >= UPLOAD_MAX_FILE_SIZE: to_multipart_upload.append((filename, file_path, file_size)) else: