Amazon s3 是一种分布式的对象存储。用键值对的方式,来存储数据。其中,存入的所有数据都是一个对象(object),每一个对象都有一个键(key)存在。

具有非常方便的 web 访问接口,以及权限控制。

下面是具体读、写、判断三个接口的具体实现

1. 创建一个 bucket

class ImageFeatIO:
__read_singleton = None
__write_singleton = None
__read_count = 0
__write_count = 0 def __init__(self):
pass @staticmethod
def get_write_instance(): #创建一个 bucket 的单例
if ImageFeatIO.__write_singleton is None:
paras = get_config('config')
access_key = paras['access_key']
secret_key = paras['secret_key']
write_host = paras['file_write_host']
conn = boto.connect_s3(
aws_access_key_id=access_key,
aws_secret_access_key=secret_key,
host=write_host, is_secure=False,
calling_format=boto.s3.connection.OrdinaryCallingFormat()
)
bucket_name = paras['bucket_name']
bucket = conn.get_bucket(bucket_name)
ImageFeatIO.__write_singleton = bucket
return ImageFeatIO.__write_singleton

2. 写入文件

写入文件的时候,可以进行权限控制

官方说明如下:

  1. Create a custom ACL that grants specific rights to specific users. At the moment, the users that are specified within grants have to be registered users of Amazon Web Services so this isn’t as useful or as general as it could be.
  2. Use a “canned” access control policy. There are four canned policies defined:
    1. private: Owner gets FULL_CONTROL. No one else has any access rights.
    2. public-read: Owners gets FULL_CONTROL and the anonymous principal is granted READ access.
    3. public-read-write: Owner gets FULL_CONTROL and the anonymous principal is granted READ and WRITE access.
    4. authenticated-read: Owner gets FULL_CONTROL and any principal authenticated as a registered Amazon S3 user is granted READ access
def write_image_feature_to_file(id, imageFeaturestring):
bucket = ImageFeatIO.get_write_instance()
k = Key(bucket)
k.key = id
res = k.set_contents_from_string(imageFeaturestring)
k.set_acl('authenticated-read')
return res

3.读取(下载)文件

s3 提供了一种非常方便的 web 服务接口,可以从任何地方以 http 协议获取数据

def read_image_feature_from_file(id):
url_head = ImageFeatIO.get_read_instance()
url = url_head + '/' + id #url= http://img.3weijia.com/bucket_name/key
request = urllib2.Request(url=url)
response = urllib2.urlopen(request, timeout=20)
res_unicode = unicode(response.read())
res = res_unicode.encode('unicode-escape').decode('string_escape')
return res

4. 判断键值是否存在

def if_image_feature_exist(id):
bucket = ImageFeatIO.get_write_instance()
key = bucket.get_key(id) return key is not None

参考:

http://boto.readthedocs.io/en/latest/s3_tut.html

https://www.cnblogs.com/web424/p/6840207.html

Amazon S3 分布式存储的 python 接口实现的更多相关文章

  1. Amazon S3 API

    一.概述 Amazon s3,全称为Amazon Simple Storage  Service.EC2和S3是Amazon最早推出的两项云服务. REST,这也是比较火的一种Web服务架构.简单来说 ...

  2. Amazon S3 功能介绍

    一 .Amazon S3介绍 Amazon Simple Storage Service (Amazon S3) 是一种对象存储,它具有简单的 Web 服务接口,可用于在 Web 上的任何位置存储和检 ...

  3. python接口自动化-参数化

    原文地址https://www.cnblogs.com/yoyoketang/p/6891710.html python接口自动化 -参数关联(一)https://www.cnblogs.com/11 ...

  4. asp.net core系列 69 Amazon S3 资源文件上传示例

    一.  上传示例 Install-Package AWSSDK.S3 -Version 3.3.104.10 using Amazon; using Amazon.Runtime; using Ama ...

  5. centos6.8 上传文件到amazon s3

    centos6.8 上传文件到amazon s3 0.参考 AWS CLI Cinnabd Reference Possible to sync a single file with aws s3 s ...

  6. python接口测试之mock(二)

    上一篇对mock-server已经做了初步的介绍,今天这里继续接着之前的介绍进行,我们先看之前的mock-server部分,之前编写了一个登录的mock,具体json文件见如下的内容: 小王子1110 ...

  7. 在windows下使用s3cmd和s3browser来管理amazon s3的笔记

    S3是Amazon S3的简称,s3cmd是一款命令行工具用来管理s3,同时还有一款图形化的管理工具:s3 browser. 因为绝大多数用户都是在linux下管理s3,而我们的打包机是在window ...

  8. caffe的python接口学习(7):绘制loss和accuracy曲线

    使用python接口来运行caffe程序,主要的原因是python非常容易可视化.所以不推荐大家在命令行下面运行python程序.如果非要在命令行下面运行,还不如直接用 c++算了. 推荐使用jupy ...

  9. Windows+Caffe+VS2013+python接口配置过程

    前段时间在笔记本上配置了Caffe框架,中间过程曲曲折折,但由于懒没有将详细过程总结下来,这两天又在一台配置较高的台式机上配置了Caffe,配置时便非常后悔当初没有写到博客中去,现已配置好Caffe, ...

随机推荐

  1. JavaScript权威指南第六版(阅读笔记)

    前言: 对于软件行业学习是无止境的,因为知识更替非常快,能够快速稳固掌握一门新技术是一个程序员应该具备的基本素质. 了解一门语言,了解它的概念非常重要,但是一些优秀的设计思想需要细心和大量实践才能慢慢 ...

  2. yum只下载不安装软件包

    一.通过yum自带的工具yumdownloader [root@host---- interpreter]# rpm -ql yum-utils package yum-utils is not in ...

  3. Prim算法与Kruskal(没有代码)

    两个最小生成树算法, 都有一个共同的思想: 这棵树是一点一点长大的; 并且每次生长, 都是贪心的. 不同之处是: Kruscal算法是以边为中心的, 每次找最小的并且有用的边添加到树上; Prim算法 ...

  4. word 2010 页眉 http://jingyan.baidu.com/article/a65957f4b0208624e77f9b55.html

    word 2010 页眉 http://jingyan.baidu.com/article/a65957f4b0208624e77f9b55.html

  5. 洛谷P2472 [SCOI2007]蜥蜴 题解

    题目链接: https://www.luogu.org/problemnew/show/P2472 分析: 这道题用最大流解决. 首先构建模型. 一根柱子可以跳入和跳出,于是拆成两个点:入点和出点. ...

  6. NOIP2018普及T4暨洛谷P5018 对称二叉树题解

    题目链接:https://www.luogu.org/problemnew/show/P5018 花絮:这道题真的比历年的t4都简单的多呀,而且本蒟蒻做得出t4做不出t3呜呜呜... 这道题可以是一只 ...

  7. html提示框插件

    最近工作需要,用到各式各样的提示框,寻找了很久,发现一个的第三方的插件很好用,各种样式.接口良好.允许自定义. 官网:http://layer.layui.com/ 使用需要先引入jq1.8以上: & ...

  8. 【Gym - 101002F】Mountain Scenes(dp)

    Mountain Scenes Descriptions 给你一个长度为n的丝带,一个宽w一个高h 的 格子,用丝带去填充格子,这填充后只需要满足至少有一列的丝带长度与其他格子不同即可.丝带可以不全部 ...

  9. 利用DOMNodeInserted监听标签内容变化

    var exeFlag = 0;//控制执行业务次数标记$('#list1').bind('DOMNodeInserted', function () { if(!/img/.test($(" ...

  10. Redis(一)--- 概述

    使用redis很长时间了,但只是限于使用,一直也没有进行系统的学习与底层研究,现系统的学习一下redis并记录笔记. Redis 1.Nosql 非关系型数据库,key-value键值对存贮. 2.内 ...