[英]I can't upload a file to google cloud using python script?
I am trying the python script available on GCP reference to upload file to google cloud storage but every time I run it I get the error of file not found for the json key credential file even though it is in the same directory as the python script. 我正在尝试在GCP参考上提供的python脚本将文件上传到谷歌云存储,但每次运行它时都会收到json密钥凭证文件找不到的文件错误,即使它与python脚本位于同一目录中。
The error is : 错误是:
File "c:\\users\\kundan\\appdata\\local\\programs\\python\\python37-
文件“c:\\ users \\ kundan \\ appdata \\ local \\ programs \\ python \\ python37-
32\\lib\\site- packages\\google\\cloud\\client.py", line 75, in from_service_account_json with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi: FileNotFoundError: [Errno 2] No such file or directory: 'key.json'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'
The code is as follows: 代码如下:
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)
This is the code I used to upload a abc.txt
file to google storage. 这是我用来将
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"))
And, below is the output: 而且,以下是输出:
Initially, I was also getting the same error as you were: 最初,我也遇到了和你一样的错误:
Traceback (most recent call last): File "gcp_upload.py", line 9, in client = storage.Client.from_service_account_json('key.json') File "E:\\Anaconda3\\envs\\py35\\lib\\site-packages\\google\\cloud\\client.py", line 75, in from_service_account_json with io.open(json_credentials_path, "r", encoding="utf-8") as json_fi: FileNotFoundError: [Errno 2] No such file or directory: 'key.json'
回溯(最近一次调用最后一次):文件“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”
I found the mistake I was making initially. 我发现了我最初犯的错误。
Check if the google storage credential json name is key.json
or key.json.json
. 检查google存储凭据json名称是否为
key.json
或key.json.json
。 It might be possible that while renaming the original google storage credential json file, you named it key.json
, but .json
extension is automatically applied after renaming, so it would have been saved as key.json.json
file, but you are passing key.json
in storage.Client.from_service_account_json()
(which actually needs to be key.json.json
). 这可能是可能的,而重命名原来的谷歌存储凭证JSON文件,将其命名为
key.json
,但.json
扩展名重命名后自动应用,所以它会被保存为key.json.json
文件,但你是路过storage.Client.from_service_account_json()
key.json
(实际上需要是key.json.json
)。 Try checking it using ls
or dir
command. 尝试使用
ls
或dir
命令检查它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.