![](/img/trans.png)
[英]Can't Upload python script on Google Cloud Console. (Error response [13] && Error response [9])
[英]I can't upload a file to google cloud using python script?
我正在尝试在GCP参考上提供的python脚本将文件上传到谷歌云存储,但每次运行它时都会收到json密钥凭证文件找不到的文件错误,即使它与python脚本位于同一目录中。
错误是:
文件“c:\\ users \\ kundan \\ appdata \\ local \\ programs \\ python \\ python37-
32 \\ lib \\ site- packages \\ google \\ cloud \\ client.py“,第75行,在from_service_account_json中,io.open(json_credentials_path,”r“,encoding =”utf-8“)为json_fi:FileNotFoundError:[Errno 2]没有这样的文件或目录:'key.json'
代码如下:
from google.cloud import storage
def upload_blob(bucket_name, source_file_name, destination_blob_name):
storage_client = storage.Client.from_service_account_json(
'key.json')
bucket = storage_client.get_bucket(bucket_name)
blob = bucket.blob(destination_blob_name)
blob.upload_from_filename(source_file_name)
print('File {} uploaded to {}.'.format(
source_file_name,
destination_blob_name))
bucket='synersense_data'
source_file_name='gcp.txt'
destination_blob_name='prototype'
upload_blob(bucket,source_file_name,destination_blob_name)
这是我用来将abc.txt
文件上传到谷歌存储的代码。
from google.cloud import storage
client = storage.Client.from_service_account_json('key.json')
def upload_blob(bucket_name, blob_name, filename):
"""
Upload a blob to the bucket.
filename: source file name
blob_name: destination blob name
"""
bucket = client.get_bucket(bucket_name)
blob = bucket.blob(blob_name)
blob.upload_from_filename(filename)
print(upload_blob("XXXXXXXXXXXXXXX", "temp/abc.txt", "abc.txt"))
而且,以下是输出:
最初,我也遇到了和你一样的错误:
回溯(最近一次调用最后一次):文件“gcp_upload.py”,第9行,在client = storage.Client.from_service_account_json('key.json')文件“E:\\ Anaconda3 \\ envs \\ py35 \\ lib \\ site-packages \\ 'google \\ cloud \\ client.py“,第75行,在from_service_account_json中,io.open(json_credentials_path,”r“,encoding =”utf-8“)为json_fi:FileNotFoundError:[Errno 2]没有这样的文件或目录:'key以.json”
我发现了我最初犯的错误。
检查google存储凭据json名称是否为key.json
或key.json.json
。 这可能是可能的,而重命名原来的谷歌存储凭证JSON文件,将其命名为key.json
,但.json
扩展名重命名后自动应用,所以它会被保存为key.json.json
文件,但你是路过storage.Client.from_service_account_json()
key.json
(实际上需要是key.json.json
)。 尝试使用ls
或dir
命令检查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.