繁体   English   中英

如何从谷歌云存储中下载 Python 或 Linux 中的文件?

[英]How to download a file in Python or Linux from Google Cloud storage?

给定属于下载按钮的公共下载 url 作为

url = "https://storage.cloud.google.com/gresearch/maxim/ckpt/Enhancement/FiveK/checkpoint.npz"

是否可以使用curlpythonwget中的任何一个下载并保存文件?

我尝试使用:

!pip install google-cloud-storage
!pip install --upgrade google-cloud-storage

from google.cloud import storage
import os

# Instantiate a CGS client
storage_client=storage.Client()
bucket_name = 'gresearch'
folder='/maxim/ckpt/Enhancement/FiveK/'
delimiter='/'
file = 'checkpoint.npz'

# Retrieve all blobs with a prefix matching the file.
bucket=storage_client.get_bucket(bucket_name)
# List blobs iterate in folder 
blobs=bucket.list_blobs(prefix=file, delimiter=delimiter) # Excluding folder inside bucket
for blob in blobs:
   print(blob.name)
   destination_uri = '{}/{}'.format(folder, blob.name) 
   blob.download_to_filename(destination_uri)

但是一个接一个地出现很多错误。 还有别的办法吗?

您的代码(按原样)无法在 Google Cloud 环境之外运行,因为您需要某种形式的身份验证来实例化您的 Cloud Storage Client。

如果您查看 Google Cloud Storage 的文档,credentials 是一个可选参数,您在实例化客户端时传递该参数,文档中有关于“credentials”的说明

credentials (Credentials) –(可选)用于此客户端的 OAuth2 凭证。 如果未通过(并且未通过 _http object),则回退到从环境推断的默认值

回到您最初的问题,如果您找到一种方法将credentials传递给您的curl or wget or just plain python ,那么您应该能够做您想做的事。 一种可能的方法是通过环境变量传递凭据(请参阅文档)。 另外,请查看此链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM