我正在編寫一個 Python 腳本,該腳本通過 Athena 運行查詢,將其輸出到 S3 并將其下載到我的計算機中。我能夠通過 Athena 運行我的查詢并將結果輸出到 S3。所以我似乎無法弄清楚的下一步是如何在不知道密鑰名稱的情況下將其下載到我的計算機上?有沒有辦法在輸出到 Athena 后在我的 python 腳本中查找對象鍵?我已經完成的:# Output location and DBs3_output = ‘s3_output_here’database = ‘database_here’# Function to run Athena querydef run_query(query, database, s3_output): while True: try: response = client.start_query_execution( QueryString=query, QueryExecutionContext={ 'Database': database }, ResultConfiguration={ 'OutputLocation': s3_output, } ) return response break except client.exceptions.TooManyRequestsException as e: print('Too many requests, trying again after sleep') time.sleep(100)# Our SQL Query query = """SELECT *FROM test”””print("Running query to Athena...")res = run_query(query, database, s3_output)我了解如何使用此代碼下載文件:try: s3.Bucket(BUCKET_NAME).download_file(KEY, ‘KEY_HERE’)except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404": print("The object does not exist.") else: raise那么如何在運行我的第一個完整代碼后讀取密鑰名稱?
1 回答

富國滬深
TA貢獻1790條經驗 獲得超9個贊
您可以使用 boto 庫提供的 get_key 命令獲取密鑰。這是我從 s3 下載東西的方式:
with open("path/aws-credentials.json") as f:
data= json.load(f)
conn = boto.connect_s3(data["accessKeyId"], data["secretAccessKey"])
bucket = conn.get_bucket('your_bucket')
file_path = bucket.get_key('path/to/s3/file')
file_path.get_contents_to_filename('path/on/local/computer/filename')
如果您只是在測試某些東西,您可以將您的憑據硬編碼到代碼中,但如果您計劃將其投入生產,最好將您的憑據存儲在外部,例如 json 文件。
添加回答
舉報
0/150
提交
取消