Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将pandas dataframe变量从google compute engine保存到google存储桶,而不首先保存到磁盘_Python_Pandas_Google Cloud Platform_Google Cloud Storage - Fatal编程技术网

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')