使用 Google Cloud Functions 时如何为 Google Cloud Datastore 实体设置日期 属性?
How to set date property for Google Cloud Datastore entity when using Google Cloud Functions?
使用 Cloud Functions 在 Cloud Datastore 中创建实体时,找不到为 属性 设置 'Date time' 的有效方法
我正在使用 Python 3.7 Cloud Function,它以 JSON 文件作为输入。 JSON 有一个对象数组,一个一个地读取这些对象以创建一个数据存储实体对象,然后将其写入数据存储。
在数据存储中,所有属性都存储为 'string'。我希望日期字段存储为 Cloud Datastore 实体的 'Date time' 属性。
dsclient = datastore.Client(os.environ.get('project_id'))
client = storage.Client()
bucket = client.get_bucket(event['bucket'])
blob = bucket.blob(event['name'])
input_bytes = blob.download_as_string()
input_string = input_bytes.decode("utf-8")
obj = json.loads(input_string)
length = len(obj[kind])
for i in range(0, length):
key = obj[kind][i]['Key']
score = create_score(obj[kind][i], key, ns)
dsclient.put(score)
def create_score(score, key, ns):
"""creates the score datastore entity"""
pkey = dsclient.key(kind, key, namespace='test')
score_entity = datastore.Entity(
key=pkey,
exclude_from_indexes=exclude_index_list
)
score_entity.update(score)
return score_entity
语句score_entity.update(score)
将所有属性创建为字符串。有没有一种方法可以在创建时指定每个 属性 的数据类型?
我已经看到在 App Engine 上使用 Python nbd
模型是可能的。但是我没有使用 App Engine 创建实体,它是一个 Cloud Function。
您需要确保 属性 是 datetime.datetime
,而不是 datetime.date
或其他类型的对象(包括字符串)。
您应该可以执行以下操作:
score_entity = datastore.Entity(
key=pkey,
exclude_from_indexes=exclude_index_list
)
score_entity["your_datetime"] = datetime.datetime.now()
...
client.put(score_entity)
使用 Cloud Functions 在 Cloud Datastore 中创建实体时,找不到为 属性 设置 'Date time' 的有效方法
我正在使用 Python 3.7 Cloud Function,它以 JSON 文件作为输入。 JSON 有一个对象数组,一个一个地读取这些对象以创建一个数据存储实体对象,然后将其写入数据存储。 在数据存储中,所有属性都存储为 'string'。我希望日期字段存储为 Cloud Datastore 实体的 'Date time' 属性。
dsclient = datastore.Client(os.environ.get('project_id'))
client = storage.Client()
bucket = client.get_bucket(event['bucket'])
blob = bucket.blob(event['name'])
input_bytes = blob.download_as_string()
input_string = input_bytes.decode("utf-8")
obj = json.loads(input_string)
length = len(obj[kind])
for i in range(0, length):
key = obj[kind][i]['Key']
score = create_score(obj[kind][i], key, ns)
dsclient.put(score)
def create_score(score, key, ns):
"""creates the score datastore entity"""
pkey = dsclient.key(kind, key, namespace='test')
score_entity = datastore.Entity(
key=pkey,
exclude_from_indexes=exclude_index_list
)
score_entity.update(score)
return score_entity
语句score_entity.update(score)
将所有属性创建为字符串。有没有一种方法可以在创建时指定每个 属性 的数据类型?
我已经看到在 App Engine 上使用 Python nbd
模型是可能的。但是我没有使用 App Engine 创建实体,它是一个 Cloud Function。
您需要确保 属性 是 datetime.datetime
,而不是 datetime.date
或其他类型的对象(包括字符串)。
您应该可以执行以下操作:
score_entity = datastore.Entity(
key=pkey,
exclude_from_indexes=exclude_index_list
)
score_entity["your_datetime"] = datetime.datetime.now()
...
client.put(score_entity)