Python 将pandas dataframe变量从google compute engine保存到google存储桶,而不首先保存到磁盘
我有一个名为Python 将pandas dataframe变量从google compute engine保存到google存储桶,而不首先保存到磁盘,python,pandas,google-cloud-platform,google-cloud-storage,Python,Pandas,Google Cloud Platform,Google Cloud Storage,我有一个名为google-storage-bucket-1的谷歌云存储桶 我连接了我的计算引擎实例,我有一个pandas数据帧变量,它是在python中创建的一个名为df1的临时变量 我想将数据帧作为csv文件保存到bucket中。我使用以下命令 import pandas as pd df1.to_csv('gs://google-storge-bucket-1/test/dataframe1.csv') 但我得到了以下错误 OSError: Forbidden: https://www
google-storage-bucket-1
的谷歌云存储桶
我连接了我的计算引擎实例,我有一个pandas数据帧变量,它是在python中创建的一个名为df1的临时变量
我想将数据帧作为csv文件保存到bucket中。我使用以下命令
import pandas as pd
df1.to_csv('gs://google-storge-bucket-1/test/dataframe1.csv')
但我得到了以下错误
OSError: Forbidden: https://www.googleapis.com/upload/storage/v1/b/xxx/o
Insufficient Permission
在不先将文件保存到磁盘的情况下,将文件保存到bucket的正确命令是什么?如下所示:
from google.cloud import storage
import os
from io import StringIO
f = StringIO() ## this is to avoid creating local file
df1.to_csv(f)
f.seek(0)
gcs = storage.Client()
gcs.get_bucket('google-storge-bucket-1').blob('dataframe1.csv').upload_from_file(f, content_type='text/csv')
像这样:
from google.cloud import storage
import os
from io import StringIO
f = StringIO() ## this is to avoid creating local file
df1.to_csv(f)
f.seek(0)
gcs = storage.Client()
gcs.get_bucket('google-storge-bucket-1').blob('dataframe1.csv').upload_from_file(f, content_type='text/csv')