python google cloud storage download error - Request failed with status code 404

반응형

python을 이용하여 google cloud storage에서 파일을 다운로드 받을때 다음과 같이 오류가 발생하는 경우가 있습니다.

 

python의 코드를 이용하여 실행하였는데, Google 공식 사이트에 안내된 스크립트를 이용하였는데, 오류가 발생합니다.

URL : cloud.google.com/storage/docs/downloading-objects?hl=ko#code-samples

 

객체 다운로드  |  Cloud Storage  |  Google Cloud

이 페이지에서는 Cloud Storage 버킷에서 객체를 다운로드하는 방법을 설명합니다. 객체에 대한 개요는 핵심 용어를 참조하세요. 참고: 객체에 고객 제공 암호화 키를 사용하는 경우 고객 제공 암호

cloud.google.com

아래의 코드를 그대로 실행하였는데, 오류가 발생하였습니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from google.cloud import storage
 
def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    # bucket_name = "your-bucket-name"
    # source_blob_name = "storage-object-name"
    # destination_file_name = "local/path/to/file"
 
    storage_client = storage.Client()
 
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)
 
    print(
        "Blob {} downloaded to {}.".format(
            source_blob_name, destination_file_name
        )
    )
cs

 

google에서 찾아본 결과 권한 문제 또는 파일이 없다고 하는 오류라고 합니다. 

권한 문제는 확실히 아녔습니다. list_blob이 정상 수행되기 때문에 download가 안될 리가 없었습니다.

결론적으로 파일 명칭이 잘못된 것이었습니다.

 

항상 Google Cloud Storage에서 주소를 보면 다음과 같은 형태입니다.

gs://bucket-name/

그런데 이 코드를 사용하기 위해서는 앞부분의 내용을 빼고 안쪽의 폴더 이름부터 시작해야 합니다.

예를 들면 파일의 위치가 다음과 같다면....

gs://bucket-name/2020/09/01/03/sample-file

다음과 같이 source_blob_name에 넣어주셔야 합니다.

2020/09/01/03/sample-file

 

위와 같이 변경하면 잘 작동합니다.

반응형