|
| 1 | +import os.path |
| 2 | +from cn_s3_api.api import CNS3Api |
| 3 | +import argparse |
| 4 | +import configparser |
| 5 | +import logging |
| 6 | + |
| 7 | + |
| 8 | +__CONFIG__=configparser.RawConfigParser() |
| 9 | +__SECTION__='' |
| 10 | +def read_config(file): |
| 11 | + with open(file) as f: |
| 12 | + file_content = f.read() |
| 13 | + __CONFIG__.read_string(file_content) |
| 14 | + return __CONFIG__ |
| 15 | + |
| 16 | +def config(section,key,default=''): |
| 17 | + c=__CONFIG__[section] |
| 18 | + return c.get(key,default) |
| 19 | + |
| 20 | +def get_config(): |
| 21 | + items=__CONFIG__.items('config') |
| 22 | + data = {} |
| 23 | + for k, v in items: |
| 24 | + data[k] = v |
| 25 | + return data |
| 26 | + |
| 27 | +def connect_s3(): |
| 28 | + s3_api = CNS3Api(dict( |
| 29 | + aws_access_key_id=config(__SECTION__, 'access_key'), |
| 30 | + aws_secret_access_key=config(__SECTION__, 'secret_key'), |
| 31 | + endpoint_url=config(__SECTION__, 'end_point'), |
| 32 | + region_name=config(__SECTION__, 'region'), |
| 33 | + )) |
| 34 | + return s3_api |
| 35 | +def copy_to_s3(file): |
| 36 | + s3_api = connect_s3() |
| 37 | + filename=os.path.basename(file) |
| 38 | + s3path=config(__SECTION__,'dir_dest') + "/" + filename |
| 39 | + try: |
| 40 | + s3_api.upload_file(config(__SECTION__,'bucket'), file, s3path) |
| 41 | + except Error: |
| 42 | + print("Erreur") |
| 43 | + |
| 44 | +def delete_to_s3(file): |
| 45 | + s3_api = connect_s3() |
| 46 | + filename = os.path.basename(file) |
| 47 | + s3path = config(__SECTION__, 'dir_dest') + "/" + filename |
| 48 | + try: |
| 49 | + s3_api.remove(config(__SECTION__,'bucket'),s3path) |
| 50 | + except Error: |
| 51 | + print("Erreur") |
| 52 | + |
| 53 | +def list_s3(dir): |
| 54 | + s3_api = connect_s3() |
| 55 | + return s3_api.list(config(__SECTION__,'bucket'),dir) |
| 56 | +def search_list(key,s3dir_list): |
| 57 | + dirname=config(__SECTION__, 'dir_dest') |
| 58 | + for i in s3dir_list: |
| 59 | + if (i['Key'] == dirname + "/" + key): |
| 60 | + return i |
| 61 | + return False |
| 62 | +def main(): |
| 63 | + global __SECTION__ |
| 64 | + parser = argparse.ArgumentParser() |
| 65 | + parser.add_argument('--config', help='configFile',default='config') |
| 66 | + parser.add_argument('--section', help='a section in the configfile', default='s3') |
| 67 | + args = parser.parse_args() |
| 68 | + __SECTION__=args.section |
| 69 | + read_config(args.config) |
| 70 | + format= '%(asctime)s %(message)s' |
| 71 | + logging.basicConfig(filename=config(args.section,'logfile'), level=logging.INFO, format=format) |
| 72 | + path = config(args.section, 'dir_source') |
| 73 | + #Load s3 list |
| 74 | + listdest=list_s3(config(__SECTION__, 'dir_dest') + "/") |
| 75 | + #list source directory |
| 76 | + for f in os.listdir(path): |
| 77 | + s=search_list(f,listdest) |
| 78 | + if (s != False): |
| 79 | + #/compare size |
| 80 | + size=os.stat(path +"/" + f).st_size |
| 81 | + if (size == s['Size']): |
| 82 | + print(f + " OK") |
| 83 | + else: |
| 84 | + print(f + " KO BAD SIZE" + "s3-size=" + str(s['Size']) + " /size=" + str(size)) |
| 85 | + copy_to_s3(path +"/" + f) |
| 86 | + print(f + " COPIED") |
| 87 | + else: |
| 88 | + print(f + " KO NOT FOUND") |
| 89 | + #copy |
| 90 | + copy_to_s3(path +"/" + f) |
| 91 | + print(f + " COPIED") |
| 92 | + |
| 93 | + |
| 94 | +if __name__ == "__main__": |
| 95 | + main() |
0 commit comments