https://console.cloud.google.com/apis/credentials
프로젝트 선택 > 새 프로젝트
프로젝트 선택에서 생성한 프로젝트로 변경 > 사용자 인증 정보 > 서비스 계정
서비스 계정 > 생성한 계정 클릭 > 키 > 키 추가 > 새 키 만들기 > JSON
JSON 파일이 받아짐.
https://console.cloud.google.com/welcome
프로젝트 선택
API 및 서비스 > 라이브러리
sheet 검색 하여 구글시트 사용
drive 검색 하여 구글드라이브 사용
구글 드라이브에서 스프레드 만들고 공유에 프로젝트 생성하면서 구글에서 주는 이메일 주소 넣고 편집 권한으로 추가.
일단 커넥션 정보 등록.
내가 쓰는 에어플로우 환경은 K8s인데 JSON 파일 위치 넣고 적기가 애매해서 Keyfile JSON에 해당 내용을 넣음.
airflow.providers.google.suite.hooks.sheets — apache-airflow-providers-google Documentation
airflow.apache.org
from airflow import DAG
from airflow.providers.google.suite.hooks.sheets import GSheetsHook
from airflow.operators.python_operator import PythonOperator
from datetime import timedelta, datetime, timezone
def read_sheet(**context):
# 대상 스프레드시트의 ID로 변경
spreadsheet_id = 'gcssid'
# 대상 스프레드시트에서 읽어올 부분 지정
spreadsheet_range = 'A1:C7'
# Connections에 등록한 구글클라우드 접근 정보 ID 사용
gs = GSheetsHook(gcp_conn_id='gc_ss_test')
gs.get_conn()
result = gs.get_values(spreadsheet_id, spreadsheet_range)
context['task_instance'].xcom_push(key='selected_data', value=result)
with DAG(
dag_id='google_spreadsheet_example',
owner='airflow',
schedule_interval=None,
start_date=datetime(2023, 10, 10),
) as dag:
read_sheet_task = PythonOperator(
task_id="read_sheet",
python_callable=read_sheet,
)
read_sheet_task
반응형