Python-使用JSON解析在Cloud Storage中创建Json文件

时间:2019-05-20 18:03:22

标签: python json parsing google-cloud-storage

我从SQL Server数据库中获取数据,并将数据保存为json格式的变量。我当然想直接在Cloud Storage中创建一个json文件,而不必在本地计算机上创建json文件。有可能吗?

我能够在本地计算机上创建一个json文件,但是我想通过在脚本中创建一个json对象而不是本地计算机上的文件,然后直接在Cloud Storage中创建一个文件来忽略此步骤。 / p>

db_string = "postgres://user:PWD@**.**.**.***:####/db_demo"

Base = declarative_base()
db = create_engine(db_string)

class DBFetch(RequestHandler):
    def get(self, gridname):  
        ownerid_var = self.request.headers["ownerid"]
        Session = sessionmaker(bind=db) 
        session = Session()
        Base.metadata.create_all(db)

        result = []
        record = db.execute("SELECT name, columndata, gridname, ownerid, issystem, ispublic, isactive, createdby, createdat, modifiedat, modifiedby
                       FROM col.layout WHERE (ispublic=1 OR (ispublic=0 AND ownerid=?)) AND (gridname=? AND isactive=1)", (ownerid_var,gridname))

        for row in record:
            result.append(row)

        result_json = json.dumps(result)
        self.write(result_json)
        self.finish()
        session.close()

def make_app():
    urls = [("/webcustomization/layouts/(.*)", DBFetch)]
    return Application(urls)

if __name__ == '__main__':
    app = make_app()
    app.listen(3000)
    IOLoop.instance().start()

1 个答案:

答案 0 :(得分:0)

您可能必须使用第三方boto客户端库插件,因为Cloud Storage还支持流传输。

要执行流式上传,请使用以下代码:

dst_uri = boto.storage_uri(+'/'+,'gs')

dst_uri.new_key()。set_contents_from_stream()

请查看以下链接:

https://cloud.google.com/storage/docs/streaming

https://cloud.google.com/storage/docs/boto-plugin#streaming-transfers