python boto和boto3操作bucket
boto操作
import datetimeimport 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 urllib3import 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'))
更多操作,请查阅官方文档 https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/s3.html
配置桶策略时,是要传入json字符串的,如果配置中存在重复的key,会失败,正确的配置方法如下
1. 对于存在多个StringEquals,但是每个里面的key都不同的情况,可以将多个键值对放到同一个StringEquals
正确写法如下
2. 对于存在多个StringEquals,但是每个里面的key都相同,但是value不同的情况,可以将多个值放到value中
正确写法如下
"Condition": {{
" StringNotEquals":{
" aws:sourceVpc":[" vpc-111bbccc"," vpc-111bbddd"]
}
以上是 python boto和boto3操作bucket 的全部内容, 来源链接: utcz.com/z/386814.html