如何使用python从谷歌云存储下载文件

时间:2021-07-11 19:03:01

标签: python linux google-cloud-storage software-design

我正在通过 Python 使用 Google Cloud Storage,所以想知道如何通过 Python 代码下载文件。 我知道,文档中提供了一个类似的示例,但想知道通过凭据进行身份验证的最佳方法。

1 个答案:

答案 0 :(得分:3)

第 1 步: 安装:

pip3 install google-cloud-storage
pip install --upgrade google-cloud-storage

第 2 步: 其次是 authenticating 自己使用凭据(如项目 ID、私钥、私钥_id、客户电子邮件和客户 ID)。

第 3 步: 来自 Downloading objects 的官方文档:

def download_blob(bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob."""

    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 file path {}. successfully ".format(
            source_blob_name, destination_file_name
        )
    )