pythonスクリプトによるアクセスログのマージ


                         ,           ,          
         

1.     python sdk
pip install nos-python-sdk

2.  python   
#!/usr/bin/evn python
# -*- coding:utf-8 -*-
import nos #  sdk
import time
import sys
import os
import re

access_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
secret_key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
bucket = "access-log"  #        
date = time.strftime('%Y-%m-%d',time.localtime(time.time() - 24*60*60))
prefix = 'aoshu'+date
client = nos.Client(access_key, secret_key)
def get_object_lists():
    try:
        object_lists = client.list_objects(bucket, prefix=prefix)
        file_lists = []
        for object_list in object_lists["response"].findall("Contents"):
            file_lists.append(object_list.find("Key").text)
            print object_list.find("Key").text
        if not file_lists:
            print "              ,    ..."
            sys.exit(1)
        else:
            return file_lists
    except nos.exceptions.ServiceException as e:
        print (
            "ServiceException: %s
"             "status_code: %s
"             "error_type: %s
"             "error_code: %s
"             "request_id: %s
"             "message: %s
"         ) % (             e,             e.status_code,                e.error_type,                e.error_code,              e.request_id,             e.message        )     except nos.exceptions.ClientException as e:         print (             "ClientException: %s
"             "message: %s
"          ) % (             e,             e.message          )          def save_log():     objects_lists = get_object_lists()     log_file_name = "nos.log"     with open(log_file_name,'a') as f:         for object in objects_lists:             try:                 result = client.get_object(bucket,object)                 f.write(result.get("body").read())             except nos.exceptions.ServiceException as e:                 print (                 "ServiceException: %s
"                 "status_code: %s
"                 "error_type: %s
"                 "error_code: %s
"                 "request_id: %s
"                 "message: %s
"                  ) % (                  e,                  e.status_code,                   e.error_type,                    e.error_code,                   e.request_id,                  e.message                          )             except nos.exceptions.ClientException as e:                 print (                 "ClientException: %s
"                 "message: %s
"                  ) % (                  e,                  e.message                    )     return log_file_name if __name__=='__main__':     save_log()