GCP Storage 파일을 Python으로 다운로드 하기

 구글 클라우드 Google Cloud Platform의 Storage에서 파일을 Python을 내려 받는방법 공유한다. 



1. Cloud Storage에서 Bucket을 생성

2. 생성한 버켓에서 테스트 파일 업로드 

3. IAM & Admin - Service Account에서 새로운 어카운트를 만든다. 여기서 한참 걸림. 만들 때 옵션으로 "Owner"로 줘야 접근 권하이 생김. 일단 뭐가 뭔지 모르는 상황에서 해결책이라 다른 권한 실험 해볼 필요 있음 

    Grant this service account access to project (optional)

4. 코드는 간단하다. 파이썬 패키치 설치하시고 아래 코드로 테스트 하면 끝

환경 설정에 GOOGLE_APPLICATION_CREDENTIALS 이란 것이 설정되어야 동작한다. 

import os
os.environ ["GOOGLE_APPLICATION_CREDENTIALS"] = "키로생성한파일.json"
print('Credendtials from environ: {}'.format(os.environ.get('GOOGLE_APPLICATION_CREDENTIALS')))

패키지 임포트

# Imports the Google Cloud client library
from google.cloud import storage

# Instantiates a client
client = storage.Client()

버켓 접근 

# Retrieve an existing bucket
# https://console.cloud.google.com/storage/browser/[bucket-id]/
bucket = client.get_bucket('너의_스토리지_버킷이름')

파일 접근 (권한 설정이 제대로 안되어 있으면 여기서 접근 거부)

blob = bucket.get_blob('다운로드받을파일이름.txt')

파일 저장

blob.download_to_filename('저장할파일이름.txt')


참고

- https://medium.com/@sandeepsinh/multiple-file-download-form-google-cloud-storage-using-python-and-gcs-api-1dbcab23c44 



Comments

Popular posts from this blog