如何将图像与Google App Engine中的实体相关联

时间:2022-05-27 07:27:50

I'm working on a Google App Engine application, and have come to the point where I want to associate images on the filesystem to entities in the database.

我正在开发一个Google App Engine应用程序,并且已经达到了我想要将文件系统上的图像与数据库中的实体相关联的程度。

I'm using the bulkupload_client.py script to upload entities to the database, but am stuck trying to figure out how to associate filesystem files to the entities. If an entity has the following images: main,detail,front,back I think I might want a naming scheme like this: <entity_key>_main.jpg

我正在使用bulkupload_client.py脚本将实体上传到数据库,但我试图弄清楚如何将文件系统文件与实体关联。如果一个实体有以下图像:main,detail,front,back我想我可能想要这样的命名方案: _main.jpg

I suppose I could create a GUID for each entity and use that, but I'd rather not have to do that.

我想我可以为每个实体创建一个GUID并使用它,但我宁愿不必那样做。

Any ideas?

I think I can't use the entity key since it might be different between local and production datastores, so I would have to rename all my images after a production bulkupload.

我想我不能使用实体密钥,因为本地数据存储区和生产数据存储区之间可能有所不同,所以我必须在生产bulkupload之后重命名所有图像。

3 个解决方案

#1


There is a GAE tutorial on how to Serve Dynamic Images with Google App Engine. Includes explanations & downloadable source code.

有关如何使用Google App Engine提供动态图像的GAE教程。包括解释和可下载的源代码。

#2


I see two options here based on my very limited knowledge of GAE.

基于我对GAE的非常有限的了解,我在这里看到两个选项。

First, you can't actually write anything to the file system in GAE, right? That would mean that any images you want to include would have to be uploaded as a part of your webapp and would therefore have a static name and directory structure that is known and unchangeable. In this case, your idea of _main.jpg, OR /entity_key/main.jpg would work fine.

首先,您无法在GAE中实际向文件系统写入任何内容,对吧?这意味着您要包含的任何图像都必须作为Web应用程序的一部分上载,因此具有已知且不可更改的静态名称和目录结构。在这种情况下,您对_main.jpg,OR /entity_key/main.jpg的想法可以正常工作。

The second option is to store the images as a blob in the database. This may allow for uploading images dynamically rather than having to upload a new version of the webapp every time you need to update images. It would quickly eat into your free database space. Here's some information on serving pictures from the database. http://code.google.com/appengine/articles/images.html

第二种选择是将图像作为blob存储在数据库中。这可以允许动态上载图像,而不是每次需要更新图像时都必须上传新版本的webapp。它很快会进入你的免费数据库空间。以下是有关从数据库提供图片的一些信息。 http://code.google.com/appengine/articles/images.html

#3


If you're uploading the images statically, you can use the key based scheme if you want: Simply assign a key name to the entities, and use that to find the associated images. You specify a key name (in the Python version) as the key_name constructor argument to an entity class:

如果您要静态上传图像,则可以根据需要使用基于密钥的方案:只需为实体分配密钥名称,然后使用该名称查找相关图像。您将键名称(在Python版本中)指定为实体类的key_name构造函数参数:

myentity = EntityClass(key_name="bleh", foo="bar", bar=123)
myentity.put()

and you can get the key name for an entity like so:

你可以得到像这样的实体的密钥名称:

myentity.key().name()

It sounds like the datastore entities in question are basically static content, though, so perhaps you'd be better simply encoding them as literals in the source and thus having them in memory, or loading them at runtime from local datafiles, bypassing the need to query the datastore for them?

听起来有问题的数据存储区实体基本上都是静态内容,所以也许你最好简单地将它们编码为源文件中的文字,从而将它们放在内存中,或者在运行时从本地数据文件加载它们,绕过需要查询数据存储区?

#1


There is a GAE tutorial on how to Serve Dynamic Images with Google App Engine. Includes explanations & downloadable source code.

有关如何使用Google App Engine提供动态图像的GAE教程。包括解释和可下载的源代码。

#2


I see two options here based on my very limited knowledge of GAE.

基于我对GAE的非常有限的了解,我在这里看到两个选项。

First, you can't actually write anything to the file system in GAE, right? That would mean that any images you want to include would have to be uploaded as a part of your webapp and would therefore have a static name and directory structure that is known and unchangeable. In this case, your idea of _main.jpg, OR /entity_key/main.jpg would work fine.

首先,您无法在GAE中实际向文件系统写入任何内容,对吧?这意味着您要包含的任何图像都必须作为Web应用程序的一部分上载,因此具有已知且不可更改的静态名称和目录结构。在这种情况下,您对_main.jpg,OR /entity_key/main.jpg的想法可以正常工作。

The second option is to store the images as a blob in the database. This may allow for uploading images dynamically rather than having to upload a new version of the webapp every time you need to update images. It would quickly eat into your free database space. Here's some information on serving pictures from the database. http://code.google.com/appengine/articles/images.html

第二种选择是将图像作为blob存储在数据库中。这可以允许动态上载图像,而不是每次需要更新图像时都必须上传新版本的webapp。它很快会进入你的免费数据库空间。以下是有关从数据库提供图片的一些信息。 http://code.google.com/appengine/articles/images.html

#3


If you're uploading the images statically, you can use the key based scheme if you want: Simply assign a key name to the entities, and use that to find the associated images. You specify a key name (in the Python version) as the key_name constructor argument to an entity class:

如果您要静态上传图像,则可以根据需要使用基于密钥的方案:只需为实体分配密钥名称,然后使用该名称查找相关图像。您将键名称(在Python版本中)指定为实体类的key_name构造函数参数:

myentity = EntityClass(key_name="bleh", foo="bar", bar=123)
myentity.put()

and you can get the key name for an entity like so:

你可以得到像这样的实体的密钥名称:

myentity.key().name()

It sounds like the datastore entities in question are basically static content, though, so perhaps you'd be better simply encoding them as literals in the source and thus having them in memory, or loading them at runtime from local datafiles, bypassing the need to query the datastore for them?

听起来有问题的数据存储区实体基本上都是静态内容,所以也许你最好简单地将它们编码为源文件中的文字,从而将它们放在内存中,或者在运行时从本地数据文件加载它们,绕过需要查询数据存储区?