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

2024-04-19 14:48:10 发布

您现在位置:Python中文网/ 问答频道 /正文

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


Tags: 文件方法代码文档身份验证示例google
1条回答
网友
1楼 · 发布于 2024-04-19 14:48:10

步骤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
        )
    )

相关问题 更多 >