python boto和boto3操作bucket的示例


Posted in Python onOctober 30, 2020

boto操作

import datetime

import boto.s3.connection
from boto.s3.key import Key
conn = boto.connect_s3(
  aws_access_key_id="123456",
  aws_secret_access_key="123456",
  host="127.0.0.1",
  port=8080,
  is_secure=False,
  calling_format=boto.s3.connection.OrdinaryCallingFormat(),
)

str_bucket_name = "bucket_test"
conn.create_bucket(str_bucket_name) # 创建bucket

for bucket in conn.get_all_buckets(): # 获取所有bucket
  # 将实际转为本地时间
  print({"name": bucket.name, "create_date": str(datetime.datetime.strptime(bucket.creation_date, "%Y-%m-%dT%H:%M:%S.%fZ") + datetime.timedelta(hours=8))})


# 删除指定的bucket
for bucket in conn.get_all_buckets():
  if bucket.name == str_bucket_name:
    for key in bucket.list(): # 必须将bucket里清空后,才能删除掉对应的bucket
      bucket.delete_key(key.name)
    conn.delete_bucket(bucket.name)
    break

# 存储文件流或字符串中的数据
key = Key('hello.txt')

key.set_contents_from_file('/tmp/hello.txt')

使用boto进行https的连接失败,  validate_certs设置成True或False没有任何作用

is_secure为Ture时,遇到的报错如下

ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

is_secure为False时,遇到的报错如下

http.client.RemoteDisconnected: Remote end closed connection without response

遂更换了botot3

boto3,下面的示例是用的https的(boto对于https的连接不上,可能是因为我的证书是自制的,所以才找了这个包)

import urllib3
import boto3

urllib3.disable_warnings()

s3 = boto3.resource(
  service_name='s3',
  aws_access_key_id="123456",
  aws_secret_access_key="123456",
  endpoint_url='https://192.168.150.20:8080',
  verify=False
)

str_bucket_name = "bucket_test"
s3.create_bucket(Bucket=str_bucket_name)


for bucket in s3.buckets.all(): # 获取所有bucket
  # 将实际转为本地时间
  print({"name": bucket.name, "create_date": datetime.datetime.strftime(bucket.creation_date + datetime.timedelta(hours=8), "%Y-%m-%d %H:%M:%S")})

# 删除指定的bucket
for bucket in s3.buckets.all():
  if bucket.name == str_bucket_name:
    bucket.objects.all().delete()  # 等价于下面两行
    # for obj in bucket.objects.all():
    #   obj.delete()
    bucket.delete()

# 存储文件流或字符串中的数据
s3.Object('mybucket', 'hello.txt').put(Body=open('/tmp/hello.txt', 'rb'))

以上就是python boto和boto3操作bucket的示例的详细内容,更多关于python 操作bucket的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
python抓取京东商城手机列表url实例代码
Dec 18 Python
python中numpy包使用教程之数组和相关操作详解
Jul 30 Python
Redis使用watch完成秒杀抢购功能的代码
May 07 Python
Apache,wsgi,django 程序部署配置方法详解
Jul 01 Python
python中字典按键或键值排序的实现代码
Aug 27 Python
python使用正则来处理各种匹配问题
Dec 22 Python
6行Python代码实现进度条效果(Progress、tqdm、alive-progress​​​​​​​和PySimpleGUI库)
Jan 06 Python
python爬虫容易学吗
Jun 02 Python
零基础学Python之前需要学c语言吗
Jul 21 Python
Matplotlib 绘制饼图解决文字重叠的方法
Jul 24 Python
flask开启多线程的具体方法
Aug 02 Python
python 实现关联规则算法Apriori的示例
Sep 30 Python
python 多进程和协程配合使用写入数据
Oct 30 #Python
python打包生成so文件的实现
Oct 30 #Python
pytorch 移动端部署之helloworld的使用
Oct 30 #Python
把Anaconda中的环境导入到Pycharm里面的方法步骤
Oct 30 #Python
Python模拟登录和登录跳转的参考示例
Oct 30 #Python
python中watchdog文件监控与检测上传功能
Oct 30 #Python
GitHub上值得推荐的8个python 项目
Oct 30 #Python
You might like
php excel类 phpExcel使用方法介绍
2010/08/21 PHP
PHP仿博客园 个人博客(1) 数据库与界面设计
2013/07/05 PHP
ThinkPHP模板Switch标签用法示例
2014/06/30 PHP
CI框架装载器Loader.php源码分析
2014/11/04 PHP
2款PHP无限级分类实例代码
2015/11/11 PHP
Windows下PHP开发环境搭建教程(Apache+PHP+MySQL)
2016/06/13 PHP
thinkphp查询,3.X 5.0方法(亲试可行)
2017/06/17 PHP
PHP实现使用DOM将XML数据存入数组的方法示例
2017/09/27 PHP
js innerHTML 的一些问题的解决方法
2008/06/22 Javascript
ASP.NET中使用后端代码注册脚本 生成JQUERY-EASYUI的界面错位的解决方法
2010/06/12 Javascript
JQuery设置获取下拉菜单某个选项的值(比较全)
2014/08/05 Javascript
Jquery树插件zTree用法入门教程
2015/02/17 Javascript
浅析 NodeJs 的几种文件路径
2017/06/07 NodeJs
使用jQuery实现购物车结算功能
2017/08/15 jQuery
Angular 5.x 学习笔记之Router(路由)应用
2018/04/08 Javascript
vue-cli脚手架打包静态资源请求出错的原因与解决
2019/06/06 Javascript
Python入门篇之列表和元组
2014/10/17 Python
Python下的subprocess模块的入门指引
2015/04/16 Python
python getopt详解及简单实例
2016/12/30 Python
python实现textrank关键词提取
2018/06/22 Python
python3读取excel文件只提取某些行某些列的值方法
2018/07/10 Python
使用numba对Python运算加速的方法
2018/10/15 Python
最新Python idle下载、安装与使用教程图文详解
2020/11/28 Python
介绍一下EJB的分类及其各自的功能及应用
2016/08/23 面试题
Prototype是怎么扩展DOM的
2014/10/01 面试题
会计系个人求职信范文分享
2013/12/20 职场文书
人力资源管理毕业求职信
2014/08/05 职场文书
妇联领导班子剖析材料
2014/08/21 职场文书
交通违章检讨书
2014/09/21 职场文书
见习报告的格式
2014/10/31 职场文书
2014年社区个人工作总结
2014/12/02 职场文书
义诊活动通知
2015/04/24 职场文书
小学少先队活动总结
2015/05/08 职场文书
血轮眼轮回眼特效 html+css
2021/03/31 HTML / CSS
SQL SERVER实现连接与合并查询
2022/02/24 SQL Server
微信小程序实现轮播图指示器
2022/06/25 Javascript