def download(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if not isinstance(args.local_path, text_type): args.local_path = args.local_path.decode(fs_coding) if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) args.cos_path, args.local_path = concat_path(args.cos_path, args.local_path) if args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] kwargs = {} kwargs['force'] = args.force kwargs['sync'] = args.sync kwargs['num'] = min(20, args.num) kwargs['ignore'] = args.ignore.split(',') kwargs['include'] = args.include.split(',') kwargs['skipmd5'] = args.skipmd5 kwargs['delete'] = args.delete kwargs['yes'] = args.yes if args.recursive: rt = Interface.download_folder(args.cos_path, args.local_path, args.headers, **kwargs) return rt else: rt = Interface.download_file(args.cos_path, args.local_path, args.headers, **kwargs) return rt return -1 except Exception as e: logger.warn(e) return -2
def copy(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] Interface = client.op_int() _, args.cos_path = concat_path(args.source_path, args.cos_path) if not isinstance(args.source_path, str): args.source_path = args.source_path.decode(fs_coding) if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) if args.recursive: _, args.cos_path = concat_path(args.source_path, args.cos_path) if args.cos_path.endswith('/') is False: args.cos_path += '/' if args.cos_path == '/': args.cos_path = '' if Interface.copy_folder(args.source_path, args.cos_path) is True: return 0 else: return 1 else: if Interface.copy_file(args.source_path, args.cos_path) is True: return 0 else: return -1
def restore(args): try: conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args. cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() kwargs = {} kwargs['day'] = args.day kwargs['tier'] = args.tier if args.recursive: if not Interface.restore_folder(cos_path=args.cos_path, **kwargs): return 0 else: return -1 else: if not Interface.restore_file(cos_path=args.cos_path, **kwargs): return 0 else: return -1 except Exception as e: logger.warn(e) return -2
def download(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if not isinstance(args.local_path, str): args.local_path = args.local_path.decode(fs_coding) if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) args.cos_path, args.local_path = concat_path(args.cos_path, args.local_path) kwargs = {} kwargs['force'] = args.force kwargs['sync'] = args.sync kwargs['num'] = 10 kwargs['ignore'] = args.ignore.split(',') if args.recursive: rt = Interface.download_folder(args.cos_path, args.local_path, **kwargs) if rt: logger.debug( "download all files under \"{file}\" directory successfully" .format(file=to_printable_str(args.cos_path))) return 0 else: logger.debug( "download all files under \"{file}\" directory failed". format(file=to_printable_str(args.cos_path))) return -1 else: if Interface.download_file(args.cos_path, args.local_path, ** kwargs) is True: return 0 else: return -1 return -1
def upload(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] Interface = client.op_int() if not isinstance(args.local_path, str): args.local_path = args.local_path.decode(fs_coding) if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) if not os.path.exists(args.local_path): logger.warn("cannot stat '%s': No such file or directory" % to_printable_str(args.local_path)) return -1 if not os.access(args.local_path, os.R_OK): logger.warn('local_path %s is not readable!' % to_printable_str(args.local_path)) return -1 args.local_path, args.cos_path = concat_path(args.local_path, args.cos_path) kwargs = {} kwargs['sync'] = args.sync kwargs['ignore'] = args.ignore.split(',') if args.recursive: if os.path.isfile(args.local_path) is True: rt = Interface.upload_file(args.local_path, args.cos_path, args.headers, **kwargs) elif os.path.isdir(args.local_path): rt = Interface.upload_folder(args.local_path, args.cos_path, args.headers, **kwargs) if rt: logger.debug( "upload all files under \"{file}\" directory successfully" .format(file=to_printable_str(args.local_path))) return 0 else: logger.debug( "upload all files under \"{file}\" directory failed". format(file=to_printable_str(args.local_path))) return -1 else: if os.path.isdir(args.local_path): logger.warn( "\"{path}\" is a directory, use \'-r\' option to upload it please." .format(path=to_printable_str(args.local_path))) return -1 if os.path.isfile(args.local_path) is False: logger.warn("cannot stat '%s': No such file or directory" % to_printable_str(args.local_path)) return -1 if Interface.upload_file(args.local_path, args.cos_path, args.headers, **kwargs) is True: return 0 else: return -1 return -1
def delete_bucket(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if Interface.delete_bucket(): return 0 else: logger.warn("delete fail!") return -1
def list_bucket(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if Interface.get_bucket(args.cos_path): return 0 else: logger.warn("list fail!") return -1
def put_bucket_versioning(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() rt = Interface.put_bucket_versioning(args.status) if rt is True: return 0 else: logger.warn("put fail!") return -1
def get_bucket_acl(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() rt = Interface.get_bucket_acl() if rt is True: return 0 else: logger.warn("get fail!") return -1
def put_bucket_acl(args): conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() rt = Interface.put_bucket_acl(args.grant_read, args.grant_write, args.grant_full_control) if rt is True: return 0 else: logger.warn("put fail!") return -1
def list_parts(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if Interface.list_multipart_uploads(cos_path=args.cos_path): return 0 else: return -1 except Exception as e: logger.warn(e) return -2
def upload(args): try: conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if args.cos_path == "": args.cos_path = "/" Interface = client.op_int() if not isinstance(args.local_path, text_type): args.local_path = args.local_path.decode(fs_coding) if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) if not os.path.exists(args.local_path): logger.warn("cannot stat '%s': No such file or directory" % to_printable_str(args.local_path)) return -1 if not os.access(args.local_path, os.R_OK): logger.warn('local_path %s is not readable!' % to_printable_str(args.local_path)) return -1 args.local_path, args.cos_path = concat_path(args.local_path, args.cos_path) if args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] kwargs = {} kwargs['sync'] = args.sync kwargs['skipmd5'] = args.skipmd5 kwargs['ignore'] = args.ignore.split(',') kwargs['include'] = args.include.split(',') kwargs['force'] = args.force kwargs['delete'] = args.delete kwargs['yes'] = args.yes if args.recursive: if os.path.isfile(args.local_path) is True: rt = Interface.upload_file(args.local_path, args.cos_path, args.headers, **kwargs) return rt elif os.path.isdir(args.local_path): rt = Interface.upload_folder(args.local_path, args.cos_path, args.headers, **kwargs) return rt else: if os.path.isdir(args.local_path): logger.warn("\"{path}\" is a directory, use \'-r\' option to upload it please".format(path=to_printable_str(args.local_path))) return -1 if os.path.isfile(args.local_path) is False: logger.warn("cannot stat '%s': No such file or directory" % to_printable_str(args.local_path)) return -1 rt = Interface.upload_file(args.local_path, args.cos_path, args.headers, **kwargs) return rt return -1 except Exception as e: logger.wran(e) return -2
def info(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() if Interface.info_object(args.cos_path, _human=args.human): return 0 else: return -1
def abort(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() if Interface.abort_parts(cos_path=args.cos_path): return 0 else: return -1
def create_bucket(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() if Interface.create_bucket(): return 0 else: logger.warn("Create bucket fail") return -1 except Exception as e: logger.warn(e) return -2
def get_bucket_versioning(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() rt = Interface.get_bucket_versioning() if rt is True: return 0 else: logger.warn("Get bucket versioning fail") return -1 except Exception as e: logger.warn(e) return -2
def put_object_acl(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() rt = Interface.put_object_acl(args.grant_read, args.grant_write, args.grant_full_control, args.cos_path) if rt is True: return 0 else: logger.warn("put fail!") return -1
def get_object_acl(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() rt = Interface.get_object_acl(args.cos_path) if rt is True: return 0 else: logger.warn("get fail!") return -1
def delete_bucket(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() kwargs = {} kwargs['force'] = args.force if Interface.delete_bucket(**kwargs): return 0 else: logger.warn("Delete bucket fail") return -1 except Exception as e: logger.warn(e) return -2
def restore(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() if Interface.restore_object(cos_path=args.cos_path, _day=args.day, _tier=args.tier): return 0 else: return -1
def signurl(args): conf = load_conf() client = CosS3Client(conf) if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] try: Interface = client.op_int() rt = Interface.sign_url(args.cos_path, args.timeout) logger.info(rt) return True except Exception: logger.warn('geturl failed') return False
def info(args): try: conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args. cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() if not Interface.info_object(args.cos_path, _human=args.human): return 0 else: return -1 except Exception as e: logger.warn(e) return -2
def list(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, str): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() if Interface.list_objects(cos_path=args.cos_path, _recursive=args.recursive, _all=args.all, _num=args.num, _human=args.human): return 0 else: return -1
def probe(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() kwargs = {} kwargs['test_num'] = args.num kwargs['file_size'] = args.size rt = Interface.probe(**kwargs) if 0 == rt: return 0 else: logger.warn("probe failed") return -1 except Exception as e: logger.warn(e) return -2
def list(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() kwargs = {} kwargs['recursive'] = args.recursive kwargs['all'] = args.all kwargs['num'] = args.num kwargs['human'] = args.human kwargs['versions'] = args.versions if Interface.list_objects(cos_path=args.cos_path, **kwargs): return 0 else: return -1
def get_object_acl(args): try: conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args. cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) Interface = client.op_int() rt = Interface.get_object_acl(args.cos_path) if rt is True: return 0 else: logger.warn("Get object acl fail") return -1 except Exception as e: logger.warn(e) return -2
def copy(args): try: conf = load_conf() client = CosS3Client(conf) Interface = client.op_int() _, args.cos_path = concat_path(args.source_path, args.cos_path) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not isinstance(args.source_path, text_type): args.source_path = args.source_path.decode(fs_coding) if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) kwargs = {} kwargs['sync'] = args.sync kwargs['force'] = args.force kwargs['directive'] = args.directive kwargs['skipmd5'] = args.skipmd5 kwargs['ignore'] = args.ignore.split(',') kwargs['include'] = args.include.split(',') kwargs['delete'] = args.delete kwargs['yes'] = args.yes kwargs['move'] = False if args.recursive: _, args.cos_path = concat_path(args.source_path, args.cos_path) if args.cos_path.endswith('/') is False: args.cos_path += '/' if args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] if not Interface.copy_folder(args.source_path, args.cos_path, args.headers, **kwargs): return 0 else: return 1 else: if not Interface.copy_file(args.source_path, args.cos_path, args.headers, **kwargs): return 0 else: return -1 except Exception as e: logger.warn(e) return -2
def signurl(args): try: conf = load_conf() client = CosS3Client(conf) if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] try: Interface = client.op_int() rt = Interface.sign_url(args.cos_path, args.timeout) if rt: return 0 else: return -1 except Exception: logger.warn('Geturl fail') return -1 except Exception as e: logger.warn(e) return -2
def delete(args): try: conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] Interface = client.op_int() if not isinstance(args. cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) kwargs = {} kwargs['force'] = args.force kwargs['versions'] = args.versions kwargs['versionId'] = args.versionId kwargs['yes'] = args.yes if args.recursive: if args.cos_path.endswith('/') is False: args.cos_path += '/' if args.cos_path == '/': args.cos_path = '' if not Interface.delete_folder(args.cos_path, **kwargs): logger.debug("delete all files under {cos_path} successfully!".format(cos_path=to_printable_str(args.cos_path))) return 0 else: logger.debug("delete all files under {cos_path} failed!".format(cos_path=to_printable_str(args.cos_path))) return -1 else: if args.cos_path == '': logger.warn("not support delete empty path") return -1 if not Interface.delete_file(args.cos_path, **kwargs): logger.debug("delete all files under {cos_path} successfully!".format(cos_path=to_printable_str(args.cos_path))) return 0 else: logger.debug("delete all files under {cos_path} failed!".format(cos_path=to_printable_str(args.cos_path))) return -1 except Exception as e: logger.warn(e) return -2
def delete(args): conf = load_conf() client = CosS3Client(conf) while args.cos_path.startswith('/'): args.cos_path = args.cos_path[1:] Interface = client.op_int() if not isinstance(args.cos_path, text_type): args.cos_path = args.cos_path.decode(fs_coding) kwargs = {} kwargs['force'] = args.force if args.recursive: if args.cos_path.endswith('/') is False: args.cos_path += '/' if args.cos_path == '/': args.cos_path = '' if Interface.delete_folder(args.cos_path, **kwargs): logger.debug( "delete all files under {cos_path} successfully!".format( cos_path=to_printable_str(args.cos_path))) return 0 else: logger.debug( "delete all files under {cos_path} failed!".format( cos_path=to_printable_str(args.cos_path))) return -1 else: if Interface.delete_file(args.cos_path, **kwargs): logger.debug( "delete all files under {cos_path} successfully!".format( cos_path=to_printable_str(args.cos_path))) return 0 else: logger.debug( "delete all files under {cos_path} failed!".format( cos_path=to_printable_str(args.cos_path))) return -1