为pynamodb提供附加功能

pynamodb-mate的Python项目详细描述


Documentation Statushttps://travis-ci.org/MacHu-GWU/pynamodb_mate-project.svg?branch=masterhttps://codecov.io/gh/MacHu-GWU/pynamodb_mate-project/branch/master/graph/badge.svghttps://img.shields.io/pypi/v/pynamodb_mate.svghttps://img.shields.io/pypi/l/pynamodb_mate.svghttps://img.shields.io/pypi/pyversions/pynamodb_mate.svghttps://img.shields.io/badge/STAR_Me_on_GitHub!--None.svg?style=social
https://img.shields.io/badge/Link-Document-blue.svghttps://img.shields.io/badge/Link-API-blue.svghttps://img.shields.io/badge/Link-Source_Code-blue.svghttps://img.shields.io/badge/Link-Install-blue.svghttps://img.shields.io/badge/Link-GitHub-blue.svghttps://img.shields.io/badge/Link-Submit_Issue-blue.svghttps://img.shields.io/badge/Link-Request_Feature-blue.svghttps://img.shields.io/badge/Link-Download-blue.svg

欢迎使用pynamodb_mate文档

特点1。在s3中存储大型二进制对象,在dynamodb中只存储s3 uri

dynamodb对于pay-as-you-gohigh-concurrent键值数据库是一个非常好的选择。有时,您希望将大型二进制对象与dynamodb项一起存储。尤其是在网络爬虫应用程序中。但是dynamodb有一个限制,一个项不能大于250kb。你怎么能解决这个问题?

一个简单的解决方案是将大型二进制对象存储在s3中,而只将s3 uri存储在dynamodb中。pynamodb_matelibrary在pynamodbproject(python中的dynamodb orm层)之上提供了这个特性。

下面是如何定义orm层的:

frompynamodb.modelsimportModelfrompynamodb.attributesimportUnicodeAttributefrompynamodb_mate.s3_backed_attributeimport(S3BackedBinaryAttribute,S3BackedUnicodeAttribute,S3BackedMixin,s3_key_safe_b64encode,)BUCKET_NAME="my-bucket"URI_PREFIX="s3://{BUCKET_NAME}/".format(BUCKET_NAME=BUCKET_NAME)classPageModel(Model,S3BackedMixin):classMeta:table_name="pynamodb_mate-pages"region="us-east-1"url=UnicodeAttribute(hash_key=True)cover_image_url=UnicodeAttribute(null=True)# this field is for html content stringhtml_content=S3BackedUnicodeAttribute(s3_uri_getter=lambdaobj:URI_PREFIX+s3_key_safe_b64encode(obj.url)+".html",compress=True,)# this field is for image binary contentcover_image_content=S3BackedBinaryAttribute(s3_uri_getter=lambdaobj:URI_PREFIX+s3_key_safe_b64encode(obj.cover_image_url)+".jpg",compress=True,)

下面是如何将大二进制存储到s3:

url="http://www.python.org"url_cover_image="http://www.python.org/logo.jpg"html_content="Hello World!\n"*1000cover_image_content=("this is a dummy image!\n"*1000).encode("utf-8")page=PageModel(url=url,cover_image_url=url_cover_image)# create, if something wrong with s3.put_object in the middle,# dirty s3 object will be cleaned uppage.atomic_save(s3_backed_data=[page.html_content.set_to(html_content),page.cover_image_content.set_to(cover_image_content)])# update, if something wrong with s3.put_object in the middle,# partially done new s3 object will be roll backhtml_content_new="Good Bye!\n"*1000cover_image_content_new=("this is another dummy image!\n"*1000).encode("utf-8")page.atomic_update(s3_backed_data=[page.html_content.set_to(html_content_new),page.cover_image_content.set_to(cover_image_content_new),])# delete, make sure s3 object are all gonepage.atomic_delete()

安装

pynamodb_mate在pypi上发布,所以您只需要:

$ pip install pynamodb_mate

要升级到最新版本:

$ pip install --upgrade pynamodb_mate

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在XML文件中自动检测XSD模式文件?   java Netbeans禁用水平滚动   java提取数字列并排序   java从对象返回嵌套集合的正确方法是什么?   java从源代码获取项目,无需配置构建路径   如何使用JavaAPI获得AKKA for MySQL的持久性?   java如何打印2d数组时间表?   spring restful webservice中的java返回JsonObject   java在当前“while(iterator.hasNext())”循环中添加到迭代器   无法在同一包的不同类中访问具有默认access关键字的java变量   用字符串Java搜索数据   JPA存储库中的java双向关联不工作   java Junit使用者编译错误   java可以同时生成多个RDD   java如何使用WebDriver接受/解除未处理的警报?