def main(): parser = argparse.ArgumentParser(description="FDS command-line tool", epilog="Doc - http://docs.api.xiaomi.com/fds/") parser.add_argument('-m', '--method', nargs='?', metavar='method', const='put', type=str, dest='method', help='Method of the request. Can be one of put/get/delete/post/head (default: put)' ).completer = ChoicesCompleter(('put', 'get', 'delete', 'post', 'head')) parser.add_argument('-b', '--bucket', nargs='?', metavar='bucket', type=str, dest='bucket', help='Name of bucket to operate' ).completer = bucket_name_completer parser.add_argument('-o', '--object', nargs='?', metavar='object', type=str, dest='object', help='Name of object to operate' ) parser.add_argument('-r', '--region', nargs='?', metavar='region', type=str, dest='region', help='Can be one of cnbj0/cnbj1/cnbj2/awsbj0/awsusor0/awssgp0/awsde0 (default: cnbj0)' ) parser.add_argument('-e', '--end_point', nargs='?', metavar='end point', type=str, dest='end_point', help='can be [cnbj1.fds.api.xiaomi.com] or empty' ) parser.add_argument('-c', '--CDN', metavar='CDN', action='store_const', const=False, dest='CDN', default=False, help='If toggled, CDN is enabled' ) parser.add_argument('--https', metavar='https', nargs='?', dest='https', default=True, help='If toggled, https is enabled' ) parser.add_argument('--ak', nargs='?', metavar='ACCESS_KEY', dest='ak', help='Specify access key' ) parser.add_argument('--sk', nargs='?', metavar='SECRET_KEY', dest='sk', help='Specify secret key' ) parser.add_argument('-L', '--list', nargs='?', metavar='list directory', const='', type=str, dest='list_dir', help='List Bucket/Object under current user') parser.add_argument('-l', '--list_objects', nargs='?', metavar='list objects', const='', type=str, dest='list_objects', help='List Bucket/Object under current user') parser.add_argument('-d', '--data', nargs='?', metavar='data file', dest='data_file', help='file to be uploaded or stored') parser.add_argument('--offset', nargs='?', metavar='offset', type=int, const=0, default=0, dest='offset', help='offset of object to be read') parser.add_argument('--length', nargs='?', metavar='length', type=int, dest='length', const=-1, default=-1, help='length of object to be read') parser.add_argument('--metadata', nargs='?', metavar='meta data of object to be uploaded', dest='metadata', help='example: "content-type:text/json;x-xiaomi-meta-user-defined:foo"') parser.add_argument('--start', nargs='?', metavar='start mark', type=str, dest='start_mark', const=None, default=None, help='used with -l or -L option, returned object name should be *no less* than start mark in dictionary order' ) parser.add_argument('--debug', metavar='debug', action='store_const', const=True, default=False, dest='debug', help='If toggled, print debug log') argcomplete.autocomplete(parser) args = parser.parse_args() # set logging log_format = '%(asctime)-15s %(message)s' logging.basicConfig(format=log_format) global logger logger = logging.getLogger('fds.cmd') debug_enabled = args.debug if debug_enabled: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) ## read config parse_argument(args=args) check_region(region=region) check_bucket_name(bucket_name=bucket_name) global fds_config fds_config = FDSClientConfiguration(region_name=region, enable_https=enable_https, enable_cdn_for_download=enable_cdn, enable_cdn_for_upload=enable_cdn) global end_point if not end_point is None: fds_config.set_endpoint(end_point) global fds_client fds_client = GalaxyFDSClient(access_key=access_key, access_secret=secret_key, config=fds_config) try: if not (list_dir is None): if not (bucket_name is None): list_directory(bucket_name=bucket_name, object_name_prefix=list_dir, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_dir, start_mark=start_mark) elif not (list_objects is None): if not (bucket_name is None): list_object(bucket_name=bucket_name, object_name_prefix=list_objects, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_objects, start_mark=start_mark) pass else: if method == 'put': if object_name: put_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata) else: put_bucket(bucket_name) pass elif method == 'get': if object_name: get_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata, offset=offset, length=length) else: get_bucket_acl(bucket_name=bucket_name) pass elif method == 'post': post_object(data_file=data_file, bucket_name=bucket_name, metadata=metadata) pass elif method == 'delete': if object_name: delete_object(bucket_name=bucket_name, object_name=object_name) else: delete_bucket(bucket_name=bucket_name) pass elif method == 'head': if object_name: if not head_object(bucket_name=bucket_name, object_name=object_name): exit(1) else: if not head_bucket(bucket_name=bucket_name): exit(1) else: parser.print_help() print("Config:") print("put following json into ~/.config/fds/client.config") print("{") print(" \"ak\":\"ACCESS_KEY\",") print(" \"sk\":\"SECRET_KEY\",") print(" \"region\":\"REGION\",") print(" \"end_point\":\"END_POINT\" (optional)") print("}") print("Usage Example:") print("\t[create bucket]\n\t\tfds -m put -b BUCKET_NAME") print("\t[list buckets]\n\t\tfds -l") print("\t[list objects under bucket]\n\t\tfds -l -b BUCKET_NAME") print("\t[list directory under bucket]\n\t\tfds -L DIR -b BUCKET_NAME") print("\t[create object under bucket]\n\t\tfds -m put -b BUCKET_NAME -o OBJECT_NAME -d FILE_PATH") print("\t[create object with pipline]\n\t\tcat file | fds -m put -b BUCKET_NAME -o OBJECT_NAME") except Exception as e: sys.stderr.write(e.message) sys.stderr.flush() if debug_enabled: logger.debug(e, exc_info=True) exit(1)
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog="Doc - http://docs.api.xiaomi.com/fds/") parser.add_argument( '-m', '--method', nargs='?', metavar='method', const='put', type=str, dest='method', help= 'Method of the request. Can be one of put/get/delete/post/head (default: put)' ).completer = ChoicesCompleter(('put', 'get', 'delete', 'post', 'head')) parser.add_argument( '-b', '--bucket', nargs='?', metavar='bucket', type=str, dest='bucket', help='Name of bucket to operate').completer = bucket_name_completer parser.add_argument('-o', '--object', nargs='?', metavar='object', type=str, dest='object', help='Name of object to operate') parser.add_argument( '-r', '--region', nargs='?', metavar='region', type=str, dest='region', help= 'Can be one of cnbj0/cnbj1/cnbj2/awsbj0/awsusor0/awssgp0/awsde0 (default: cnbj0)' ) parser.add_argument('-e', '--end_point', nargs='?', metavar='end point', type=str, dest='end_point', help='can be [cnbj1.fds.api.xiaomi.com] or empty') parser.add_argument('-c', '--CDN', metavar='CDN', action='store_const', const=False, dest='CDN', default=False, help='If toggled, CDN is enabled') parser.add_argument('-p', '--presigned_url', action='store_true', dest='presigned_url', default=False, help='If toggled, generate presigned url') parser.add_argument('--https', metavar='https', nargs='?', dest='https', default=True, help='If toggled, https is enabled') parser.add_argument('--ak', nargs='?', metavar='ACCESS_KEY', dest='ak', help='Specify access key') parser.add_argument('--sk', nargs='?', metavar='SECRET_KEY', dest='sk', help='Specify secret key') parser.add_argument('-L', '--list', nargs='?', metavar='list directory', const='', type=str, dest='list_dir', help='List Bucket/Object under current user') parser.add_argument('-l', '--list_objects', nargs='?', metavar='list objects', const='', type=str, dest='list_objects', help='List Bucket/Object under current user') parser.add_argument('-d', '--data', nargs='?', metavar='data file', dest='data_file', help='file to be uploaded or stored') parser.add_argument( '-D', '--directory', nargs='?', metavar='data dir', dest='data_dir', help= "Directory to be uploaded or stored. Use '/' to download all objects under the bucket" ) parser.add_argument('-R', '--recursive', action='store_true', dest='recursive', default=False, help='If toggled, download the directory recursively') parser.add_argument('--offset', nargs='?', metavar='offset', type=int, const=0, default=0, dest='offset', help='offset of object to be read') parser.add_argument('--length', nargs='?', metavar='length', type=int, dest='length', const=-1, default=-1, help='length of object to be read') parser.add_argument( '--metadata', nargs='?', metavar='meta data of object to be uploaded', dest='metadata', help='example: "content-type:text/json;x-xiaomi-meta-user-defined:foo"' ) parser.add_argument( '--start', nargs='?', metavar='start mark', type=str, dest='start_mark', const=None, default=None, help= 'used with -l or -L option, returned object name should be *no less* than start mark in dictionary order' ) parser.add_argument('--debug', metavar='debug', action='store_const', const=True, default=False, dest='debug', help='If toggled, print debug log') parser.add_argument( '--expiration', nargs='?', type=str, metavar='expiration in hour', default='1.0', dest='expiration_in_hour', help= 'used with --presigned_url, set expiration of presigned url generated from now on(hour), default to one hour' ) parser.add_argument('--force', action='store_true', dest='force_delete', default=False, help='If toggled, delete bucket and objects') parser.add_argument('--disable_trash', action='store_true', dest='disable_trash', default=False, help='If toggled, delete object without move to trash') parser.add_argument('-P', '--object_prefix', nargs='?', metavar="object's prefix", type=str, dest='object_prefix', help="object's prefix") parser.add_argument( '--lifecycle', nargs='?', metavar='lifecycle config, json format', dest='lifecycle', const=True, default=None, help= '''Put or get lifecycle configof the bucket. Please use \\" instead of " in this argument when putting lifecycle config due to shell may eat double quotes.''' ) parser.add_argument( '--lifecycle-rule', nargs='?', metavar='lifecycle rule, json format', dest='lifecycle_rule', const=True, default=None, help= '''Add/update or get one rule of lifecycle config of the bucket. Please use \\" instead of " in this argument when putting lifecycle config due to shell may eat double quotes.''' ) parser.add_argument( '--webp-quality', nargs='?', dest='webp_quality', const=-1, default=None, help= 'Integer indicates webp quality, -1 will disable bucket auto convert webp' ) parser.add_argument( '--restore-archived', action='store_true', dest='restore_archived', default=False, help='''If toggled, restore archived object as standard.''') group = parser.add_argument_group('acl') group.add_argument('--gratee', nargs='+', metavar='user, group, ALL_USERS, AUTHENTICATED_USERS', dest='gratee', help='Add acl to bucket') group.add_argument( '--permission', nargs='?', metavar="READ, WRITE, READ_OBJECTS, FULL_CONTROL", dest='permission', choices=['READ', 'WRITE', 'READ_OBJECTS', 'FULL_CONTROL'], help='Add acl to bucket') cp = parser.add_argument_group('cp') cp.add_argument('-srcb', '--src_bucket', nargs='?', metavar='bucket, name', dest='src_bucket_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-srco', '--src_object', nargs='?', metavar='object, name', dest='src_object_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-dstb', '--dst_bucket', nargs='?', metavar='bucket, name', dest='dst_bucket_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-dsto', '--dst_object', nargs='?', metavar='object, name', dest='dst_object_name', help='Copy object from src_bucket to dst_bucket') argcomplete.autocomplete(parser) args = parser.parse_args() # set logging log_format = '%(asctime)-15s [%(filename)s:%(lineno)d] %(message)s' logging.basicConfig(format=log_format) global logger logger = logging.getLogger('fds.cmd') debug_enabled = args.debug if debug_enabled: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) ## read config parse_argument(args=args) check_region(region=region) check_bucket_name(bucket_name=bucket_name) global fds_config fds_config = FDSClientConfiguration( region_name=region, enable_https=enable_https, enable_cdn_for_download=enable_cdn, enable_cdn_for_upload=enable_cdn, threshold_size=multipart_upload_threshold_size, part_size=multipart_upload_buffer_size) global end_point if not end_point is None: fds_config.set_endpoint(end_point) global fds_client fds_client = GalaxyFDSClient(access_key=access_key, access_secret=secret_key, config=fds_config) global force_delete global disable_trash global object_prefix global recursive, webp_quality try: if presigned_url: expiration = int( 1000 * (float(eval(expiration_in_hour)) * 3600 + float( (datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds()))) meta = parse_metadata_from_str(metadata=metadata) content_type = None if meta and 'content-type' in meta.metadata: content_type = meta.metadata['content-type'] url = fds_client.generate_presigned_uri(fds_config.get_base_uri(), bucket_name=bucket_name, object_name=object_name, expiration=expiration, http_method=method.upper(), content_type=content_type) print(url) elif not (list_dir is None): if not (bucket_name is None): list_directory(bucket_name=bucket_name, object_name_prefix=list_dir, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_dir, start_mark=start_mark) elif not (list_objects is None): if not (bucket_name is None): if object_name is not None: list_version_ids(bucket_name=bucket_name, object_name=object_name) else: list_object(bucket_name=bucket_name, object_name_prefix=list_objects, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_objects, start_mark=start_mark) pass else: if method == 'put': if src_bucket_name and src_object_name and dst_bucket_name and dst_object_name: copy_object(src_bucket_name, src_object_name, dst_bucket_name, dst_object_name) elif object_name: if data_dir: put_directory(data_dir=data_dir, bucket_name=bucket_name, object_name_prefix=object_name, metadata=metadata) elif gratee and permission: put_object_acl(bucket_name, object_name, gratee, permission) elif restore_archived: restore_archived_object(bucket_name, object_name) else: put_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata) elif gratee and permission: put_bucket_acl(bucket_name, gratee, permission) elif lifecycle: put_bucket_lifecycle_config(bucket_name, lifecycle) elif lifecycle_rule: put_bucket_lifecycle_rule(bucket_name, lifecycle_rule) elif webp_quality: set_bucket_default_webp_quality(bucket_name=bucket_name, webp_quality=webp_quality) else: put_bucket(bucket_name) pass elif method == 'get': if object_prefix: download_directory(bucket_name=bucket_name, object_prefix=object_prefix, data_dir=data_dir, recursive=recursive) elif object_name: get_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata, offset=offset, length=length, webp_quality=webp_quality) elif lifecycle: get_bucket_lifecycle_config(bucket_name) else: get_bucket_acl(bucket_name=bucket_name) pass elif method == 'post': post_object(data_file=data_file, bucket_name=bucket_name, metadata=metadata) pass elif method == 'delete': if object_name: delete_object(bucket_name=bucket_name, object_name=object_name, enable_trash=not disable_trash) elif object_prefix is not None: delete_objects(bucket_name=bucket_name, object_prefix=object_prefix, enable_trash=not disable_trash) elif force_delete: delete_bucket_and_objects(bucket_name=bucket_name) else: delete_bucket(bucket_name=bucket_name) pass elif method == 'head': if object_name: if not head_object(bucket_name=bucket_name, object_name=object_name): exit(1) else: if not head_bucket(bucket_name=bucket_name): exit(1) else: parser.print_help() except Exception as e: print(e) print("\n") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) # sys.stderr.write(str(e)) # sys.stderr.flush() if debug_enabled: logger.debug(e, exc_info=True) exit(1)
def main(): parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog="Doc - http://docs.api.xiaomi.com/fds/") parser.add_argument('-m', '--method', nargs='?', metavar='method', const='put', type=str, dest='method', help='Method of the request. Can be one of put/get/delete/post/head (default: put)' ).completer = ChoicesCompleter(('put', 'get', 'delete', 'post', 'head')) parser.add_argument('-b', '--bucket', nargs='?', metavar='bucket', type=str, dest='bucket', help='Name of bucket to operate' ).completer = bucket_name_completer parser.add_argument('-o', '--object', nargs='?', metavar='object', type=str, dest='object', help='Name of object to operate' ) parser.add_argument('-r', '--region', nargs='?', metavar='region', type=str, dest='region', help='Can be one of cnbj0/cnbj1/cnbj2/awsbj0/awsusor0/awssgp0/awsde0 (default: cnbj0)' ) parser.add_argument('-e', '--end_point', nargs='?', metavar='end point', type=str, dest='end_point', help='can be [cnbj1.fds.api.xiaomi.com] or empty' ) parser.add_argument('-c', '--CDN', metavar='CDN', action='store_const', const=False, dest='CDN', default=False, help='If toggled, CDN is enabled' ) parser.add_argument('-p', '--presigned_url', action='store_true', dest='presigned_url', default=False, help='If toggled, generate presigned url' ) parser.add_argument('--https', metavar='https', nargs='?', dest='https', default=True, help='If toggled, https is enabled' ) parser.add_argument('--ak', nargs='?', metavar='ACCESS_KEY', dest='ak', help='Specify access key' ) parser.add_argument('--sk', nargs='?', metavar='SECRET_KEY', dest='sk', help='Specify secret key' ) parser.add_argument('-L', '--list', nargs='?', metavar='list directory', const='', type=str, dest='list_dir', help='List Bucket/Object under current user') parser.add_argument('-l', '--list_objects', nargs='?', metavar='list objects', const='', type=str, dest='list_objects', help='List Bucket/Object under current user') parser.add_argument('-d', '--data', nargs='?', metavar='data file', dest='data_file', help='file to be uploaded or stored') parser.add_argument('-D', '--directory', nargs='?', metavar='data dir', dest='data_dir', help="Directory to be uploaded or stored. Use '/' to download all objects under the bucket") parser.add_argument('-R', '--recursive', action='store_true', dest='recursive', default=False, help='If toggled, download the directory recursively') parser.add_argument('--offset', nargs='?', metavar='offset', type=int, const=0, default=0, dest='offset', help='offset of object to be read') parser.add_argument('--length', nargs='?', metavar='length', type=int, dest='length', const=-1, default=-1, help='length of object to be read') parser.add_argument('--metadata', nargs='?', metavar='meta data of object to be uploaded', dest='metadata', help='example: "content-type:text/json;x-xiaomi-meta-user-defined:foo"') parser.add_argument('--start', nargs='?', metavar='start mark', type=str, dest='start_mark', const=None, default=None, help='used with -l or -L option, returned object name should be *no less* than start mark in dictionary order' ) parser.add_argument('--debug', metavar='debug', action='store_const', const=True, default=False, dest='debug', help='If toggled, print debug log') parser.add_argument('--expiration', nargs='?', type=str, metavar='expiration in hour', default='1.0', dest='expiration_in_hour', help='used with --presigned_url, set expiration of presigned url generated from now on(hour), default to one hour') parser.add_argument('--force', action='store_true', dest='force_delete', default=False, help='If toggled, delete bucket and objects') parser.add_argument('--disable_trash', action='store_true', dest='disable_trash', default=False, help='If toggled, delete object without move to trash') parser.add_argument('-P', '--object_prefix', nargs='?', metavar="object's prefix", type=str, dest='object_prefix', help="object's prefix") parser.add_argument('--lifecycle', nargs='?', metavar='lifecycle config, json format', dest='lifecycle', const=True, default=None, help='''Put or get lifecycle configof the bucket. Please use \\" instead of " in this argument when putting lifecycle config due to shell may eat double quotes.''') parser.add_argument('--cors', nargs='?', metavar='cors config, json format', dest='cors', const=True, default=None, help='''Put or get cors config of the bucket. Please use \\" instead of " in this argument when putting cors config due to shell may eat double quotes.''') parser.add_argument('--lifecycle-rule', nargs='?', metavar='lifecycle rule, json format', dest='lifecycle_rule', const=True, default=None, help='''Add/update or get one rule of lifecycle config of the bucket. Please use \\" instead of " in this argument when putting lifecycle config due to shell may eat double quotes.''') parser.add_argument('--cors-rule', nargs='?', metavar='cors rule, json format', dest='cors_rule', const=True, default=None, help='''Add/update or get one rule of cors config of the bucket. Please use \\" instead of " in this argument when putting cors config due to shell may eat double quotes.''') parser.add_argument('--webp-quality', nargs='?', dest='webp_quality', const=-1, default=None, help='Integer indicates webp quality, -1 will disable bucket auto convert webp') parser.add_argument('--gif-extract-type', nargs='?', dest='gif_extract_type', const='unknown', default=None, help='String indicates gif extract type, unknown will disable bucket auto gif extract') parser.add_argument('--restore-archived', action='store_true', dest='restore_archived', default=False, help='''If toggled, restore archived object as standard.''') group = parser.add_argument_group('acl') group.add_argument('--gratee', nargs='+', metavar='user, group, ALL_USERS, AUTHENTICATED_USERS', dest='gratee', help='Add acl to bucket') group.add_argument('--permission', nargs='?', metavar="READ, WRITE, READ_OBJECTS, FULL_CONTROL", dest='permission', choices=['READ', 'WRITE', 'READ_OBJECTS', 'FULL_CONTROL'], help='Add acl to bucket') cp = parser.add_argument_group('cp') cp.add_argument('-srcb', '--src_bucket', nargs='?', metavar='bucket, name', dest='src_bucket_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-srco', '--src_object', nargs='?', metavar='object, name', dest='src_object_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-dstb', '--dst_bucket', nargs='?', metavar='bucket, name', dest='dst_bucket_name', help='Copy object from src_bucket to dst_bucket') cp.add_argument('-dsto', '--dst_object', nargs='?', metavar='object, name', dest='dst_object_name', help='Copy object from src_bucket to dst_bucket') argcomplete.autocomplete(parser) args = parser.parse_args() # set logging log_format = '%(asctime)-15s [%(filename)s:%(lineno)d] %(message)s' logging.basicConfig(format=log_format) global logger logger = logging.getLogger('fds.cmd') debug_enabled = args.debug if debug_enabled: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) ## read config parse_argument(args=args) check_region(region=region) check_bucket_name(bucket_name=bucket_name) global fds_config fds_config = FDSClientConfiguration(region_name=region, enable_https=enable_https, enable_cdn_for_download=enable_cdn, enable_cdn_for_upload=enable_cdn, threshold_size=multipart_upload_threshold_size, part_size=multipart_upload_buffer_size) global end_point if not end_point is None: fds_config.set_endpoint(end_point) global fds_client fds_client = GalaxyFDSClient(access_key=access_key, access_secret=secret_key, config=fds_config) global force_delete global disable_trash global object_prefix global recursive, webp_quality, gif_extract_type try: if presigned_url: expiration = int(1000 * (float(eval(expiration_in_hour)) * 3600 + float((datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds()))) meta = parse_metadata_from_str(metadata=metadata) content_type = None if meta and 'content-type' in meta.metadata: content_type = meta.metadata['content-type'] url = fds_client.generate_presigned_uri(fds_config.get_base_uri(), bucket_name=bucket_name, object_name=object_name, expiration=expiration, http_method=method.upper(), content_type=content_type) print(url) elif not (list_dir is None): if not (bucket_name is None): list_directory(bucket_name=bucket_name, object_name_prefix=list_dir, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_dir, start_mark=start_mark) elif not (list_objects is None): if not (bucket_name is None): if object_name is not None: list_version_ids(bucket_name=bucket_name, object_name=object_name) else: list_object(bucket_name=bucket_name, object_name_prefix=list_objects, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_objects, start_mark=start_mark) pass else: if method == 'put': if src_bucket_name and src_object_name and dst_bucket_name and dst_object_name: copy_object(src_bucket_name, src_object_name, dst_bucket_name, dst_object_name) elif object_name: if data_dir: put_directory(data_dir=data_dir, bucket_name=bucket_name, object_name_prefix=object_name, metadata=metadata) elif gratee and permission: put_object_acl(bucket_name, object_name, gratee, permission) elif restore_archived: restore_archived_object(bucket_name, object_name) else: put_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata) elif gratee and permission: put_bucket_acl(bucket_name, gratee, permission) elif lifecycle: put_bucket_lifecycle_config(bucket_name, lifecycle) elif lifecycle_rule: put_bucket_lifecycle_rule(bucket_name, lifecycle_rule) elif cors: put_bucket_cors_config(bucket_name, cors) elif cors_rule: put_bucket_cors_rule(bucket_name, cors_rule) elif webp_quality: set_bucket_default_webp_quality(bucket_name=bucket_name, webp_quality=webp_quality) elif gif_extract_type: set_bucket_default_gif_extract_type(bucket_name=bucket_name, gif_extract_type=gif_extract_type) else: put_bucket(bucket_name) pass elif method == 'get': if object_prefix: download_directory(bucket_name=bucket_name, object_prefix=object_prefix, data_dir=data_dir, recursive=recursive) elif object_name: get_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata, offset=offset, length=length, webp_quality=webp_quality, gif_extract_type=gif_extract_type) elif lifecycle: get_bucket_lifecycle_config(bucket_name) elif cors: get_bucket_cors_config(bucket_name) else: get_bucket_acl(bucket_name=bucket_name) pass elif method == 'post': post_object(data_file=data_file, bucket_name=bucket_name, metadata=metadata) pass elif method == 'delete': if object_name: if gratee and permission: delete_object_acl(bucket_name, object_name, gratee, permission) else: delete_object(bucket_name=bucket_name, object_name=object_name, enable_trash=not disable_trash) elif object_prefix is not None: delete_objects(bucket_name=bucket_name, object_prefix=object_prefix, enable_trash=not disable_trash) elif force_delete: delete_bucket_and_objects(bucket_name=bucket_name) else: if gratee and permission: delete_bucket_acl(bucket_name, gratee, permission) else: delete_bucket(bucket_name=bucket_name) pass elif method == 'head': if object_name: if not head_object(bucket_name=bucket_name, object_name=object_name): exit(1) else: if not head_bucket(bucket_name=bucket_name): exit(1) else: parser.print_help() except Exception as e: print(e) print("\n") ex_type, ex, tb = sys.exc_info() traceback.print_tb(tb) # sys.stderr.write(str(e)) # sys.stderr.flush() if debug_enabled: logger.debug(e, exc_info=True) exit(1)
def main(): parser = argparse.ArgumentParser( description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter, epilog="Doc - http://docs.api.xiaomi.com/fds/") parser.add_argument( '-m', '--method', nargs='?', metavar='method', const='put', type=str, dest='method', help= 'Method of the request. Can be one of put/get/delete/post/head (default: put)' ).completer = ChoicesCompleter(('put', 'get', 'delete', 'post', 'head')) parser.add_argument( '-b', '--bucket', nargs='?', metavar='bucket', type=str, dest='bucket', help='Name of bucket to operate').completer = bucket_name_completer parser.add_argument('-o', '--object', nargs='?', metavar='object', type=str, dest='object', help='Name of object to operate') parser.add_argument( '-r', '--region', nargs='?', metavar='region', type=str, dest='region', help= 'Can be one of cnbj0/cnbj1/cnbj2/awsbj0/awsusor0/awssgp0/awsde0 (default: cnbj0)' ) parser.add_argument('-e', '--end_point', nargs='?', metavar='end point', type=str, dest='end_point', help='can be [cnbj1.fds.api.xiaomi.com] or empty') parser.add_argument('-c', '--CDN', metavar='CDN', action='store_const', const=False, dest='CDN', default=False, help='If toggled, CDN is enabled') parser.add_argument('-p', '--presigned_url', action='store_true', dest='presigned_url', default=False, help='If toggled, generate presigned url') parser.add_argument('--https', metavar='https', nargs='?', dest='https', default=True, help='If toggled, https is enabled') parser.add_argument('--ak', nargs='?', metavar='ACCESS_KEY', dest='ak', help='Specify access key') parser.add_argument('--sk', nargs='?', metavar='SECRET_KEY', dest='sk', help='Specify secret key') parser.add_argument('-L', '--list', nargs='?', metavar='list directory', const='', type=str, dest='list_dir', help='List Bucket/Object under current user') parser.add_argument('-l', '--list_objects', nargs='?', metavar='list objects', const='', type=str, dest='list_objects', help='List Bucket/Object under current user') parser.add_argument('-d', '--data', nargs='?', metavar='data file', dest='data_file', help='file to be uploaded or stored') parser.add_argument('-D', '--directory', nargs='?', metavar='data dir', dest='data_dir', help='directory to be uploaded or stored') parser.add_argument('--offset', nargs='?', metavar='offset', type=long, const=0, default=0, dest='offset', help='offset of object to be read') parser.add_argument('--length', nargs='?', metavar='length', type=long, dest='length', const=-1, default=-1, help='length of object to be read') parser.add_argument( '--metadata', nargs='?', metavar='meta data of object to be uploaded', dest='metadata', help='example: "content-type:text/json;x-xiaomi-meta-user-defined:foo"' ) parser.add_argument( '--start', nargs='?', metavar='start mark', type=str, dest='start_mark', const=None, default=None, help= 'used with -l or -L option, returned object name should be *no less* than start mark in dictionary order' ) parser.add_argument('--debug', metavar='debug', action='store_const', const=True, default=False, dest='debug', help='If toggled, print debug log') parser.add_argument( '--expiration', nargs='?', type=str, metavar='expiration in hour', default='1.0', dest='expiration_in_hour', help= 'used with --presigned_url, set expiration of presigned url generated from now on(hour), default to one hour' ) argcomplete.autocomplete(parser) args = parser.parse_args() # set logging log_format = '%(asctime)-15s [%(filename)s:%(lineno)d] %(message)s' logging.basicConfig(format=log_format) global logger logger = logging.getLogger('fds.cmd') debug_enabled = args.debug if debug_enabled: logger.setLevel(logging.DEBUG) else: logger.setLevel(logging.INFO) ## read config parse_argument(args=args) check_region(region=region) check_bucket_name(bucket_name=bucket_name) global fds_config fds_config = FDSClientConfiguration(region_name=region, enable_https=enable_https, enable_cdn_for_download=enable_cdn, enable_cdn_for_upload=enable_cdn) global end_point if not end_point is None: fds_config.set_endpoint(end_point) global fds_client fds_client = GalaxyFDSClient(access_key=access_key, access_secret=secret_key, config=fds_config) try: if presigned_url: expiration = int( 1000 * (float(eval(expiration_in_hour)) * 3600 + float( (datetime.utcnow() - datetime(1970, 1, 1, 0, 0, 0, 0)).total_seconds()))) meta = parse_metadata_from_str(metadata=metadata) content_type = None if meta and 'content-type' in meta.metadata: content_type = meta.metadata['content-type'] url = fds_client.generate_presigned_uri(fds_config.get_base_uri(), bucket_name=bucket_name, object_name=object_name, expiration=expiration, http_method=method.upper(), content_type=content_type) print url elif not (list_dir is None): if not (bucket_name is None): list_directory(bucket_name=bucket_name, object_name_prefix=list_dir, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_dir, start_mark=start_mark) elif not (list_objects is None): if not (bucket_name is None): list_object(bucket_name=bucket_name, object_name_prefix=list_objects, start_mark=start_mark) else: list_buckets(fds_client=fds_client, prefix=list_objects, start_mark=start_mark) pass else: if method == 'put': if object_name: if data_dir: put_directory(data_dir=data_dir, bucket_name=bucket_name, object_name_prefix=object_name, metadata=metadata) else: put_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata) else: put_bucket(bucket_name) pass elif method == 'get': if object_name: get_object(data_file=data_file, bucket_name=bucket_name, object_name=object_name, metadata=metadata, offset=offset, length=length) else: get_bucket_acl(bucket_name=bucket_name) pass elif method == 'post': post_object(data_file=data_file, bucket_name=bucket_name, metadata=metadata) pass elif method == 'delete': if object_name: delete_object(bucket_name=bucket_name, object_name=object_name) else: delete_bucket(bucket_name=bucket_name) pass elif method == 'head': if object_name: if not head_object(bucket_name=bucket_name, object_name=object_name): exit(1) else: if not head_bucket(bucket_name=bucket_name): exit(1) else: parser.print_help() except Exception as e: sys.stderr.write(e.message) sys.stderr.flush() if debug_enabled: logger.debug(e, exc_info=True) exit(1)