我一直在将对象 pickle 到文件系统中,并在需要使用这些对象时将其读回。目前,我已经为此目的使用了此代码。
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
现在,我将应用程序(django)移至Google应用程序引擎,并发现该应用程序引擎不允许我写入文件系统。谷歌云存储似乎是我唯一的选择,但我不明白如何将对象 pickle 为云存储对象并将其读回以创建原始的python对象。
最佳答案
您可以使用Cloud Storage client library。
代替open()
,请使用cloudstorage.open()
(如果将gcs.open()
导入为cloudstorage
,则使用gcs
,如上述文档中所述),并注意完整文件路径以GCS存储桶名称(作为dir)开头。
在cloudstorage.open() documentation中有更多详细信息。