2017-11-14 2 views
0

데프 주 (target_db) 바이트 필요가 있습니다 JSON 객체로로드 라인의 스크립트 (dict_output =를 실행하는 동안JSON은 STR하지

# get available logs 

output = subprocess.Popen(['aws','rds', 'describe-db-log-files', '--db-instance-identifier', target_db],stdout=subprocess.PIPE).communicate()[0] 

dict_output = json.loads(output) 
log_file_names = map(lambda l:l['LogFileName'], dict_output['DescribeDBLogFiles']) 

mkdir_p('logs/error') 

# aws rds download-db-log-file-portion --db-instance-identifier prod-api --log-file-name error/postgresql.log.2015-04-20-18 --output text > logs/error/postgresql.log.2015-04-20-18 
for log_file_name in log_file_names: 
    content = subprocess.Popen(['aws','rds', 'download-db-log-file-portion', '--db-instance-identifier', target_db, '--log-file-name', log_file_name, '--output', 'text'],stdout=subprocess.PIPE).communicate()[0] 
    with open('logs/%s' % log_file_name, 'wb') as fw: 
     fw.write(content) 

내가 오류 JSON을 얻고있다하지 바이트를 str에해야 json.loads (출력)).

+0

당신이 사용하고있는'python 버전 '은 무엇이고'aws cli 출력 형식'은 무엇입니까? –

+0

하위 프로 시저를 만드는 대신 boto3 라이브러리를 사용하여 작업을 수행하는 것이 좋습니다. http://boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.download_db_log_file_portion 및 http : /boto3.readthedocs.io/en/latest/reference/services/rds.html#RDS.Client.describe_db_log_files –

+0

python 버전은 3.4.3입니다. –

답변

0

먼저 그런 유형을 인쇄하려고 :

print type(output)

이 문자열이 사용하여 문자열로 변환하려고하지 않은 경우 :

output = str(output) 또는 dict_output = json.loads(str(output))

희망이 뜻을 도움.