酸洗python对象到谷歌云存储 [英] pickling python objects to google cloud storage

查看:135
本文介绍了酸洗python对象到谷歌云存储的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在对文件系统进行酸洗,并在需要处理这些对象时将它们读回来。目前我有这个代码。

  def pickle(self,directory,filename):
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory +'/'+ filename,'wb')as handle:
pickle.dump(self ,处理)

@staticmethod
def load(filename):
with open(filename,'rb')as handle:
element = pickle.load(handle )
返回元素

现在我将我的应用程序(django)移动到Google应用程序引擎并认为应用引擎不允许我写入文件系统。 Google云存储似乎是我唯一的选择,但我无法理解如何将我的对象腌制为云存储对象并将其读回以创建原始python对象。 您可以使用 googlecloudstorageclient /读写云存储rel =nofollow>云存储客户端库



取代 open()使用 cloudstorage.open()(或 gcs.open() if导入 cloudstorage 作为 gcs ,如上面提到的文档)并且注意完整的文件路径以GCS桶开始名称(作为目录)。



cloudstorage.open()文档


I've been pickling the objects to filesystem and reading them back when needed to work with those objects. Currently I've this code for that purpose.

def pickle(self, directory, filename):
    if not os.path.exists(directory):
        os.makedirs(directory)
    with open(directory + '/' + filename, 'wb') as handle:
        pickle.dump(self, handle)

@staticmethod
def load(filename):
    with open(filename, 'rb') as handle:
        element = pickle.load(handle)
    return element

Now I'm moving my applictation(django) to Google app engine and figured that app engine does not allow me to write to file system. Google cloud storage seemed my only choice but I could not understand how could I pickle my objects as cloud storage objects and read them back to create the original python object.

解决方案

You can use the Cloud Storage client library.

Instead of open() use cloudstorage.open() (or gcs.open() if importing cloudstorage as gcs, as in the above-mentioned doc) and note that the full filepath starts with the GCS bucket name (as a dir).

More details in the cloudstorage.open() documentation.

这篇关于酸洗python对象到谷歌云存储的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆