python boto和boto3操作bucket的示例
boto操作
import datetimeimport boto.s3.connectionfrom boto.s3.key import Keyconn = 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) # 創(chuàng)建bucketfor bucket in conn.get_all_buckets(): # 獲取所有bucket # 將實(shí)際轉(zhuǎn)為本地時(shí)間 print({'name': bucket.name, 'create_date': str(datetime.datetime.strptime(bucket.creation_date, '%Y-%m-%dT%H:%M:%S.%fZ') + datetime.timedelta(hours=8))})# 刪除指定的bucketfor bucket in conn.get_all_buckets(): if bucket.name == str_bucket_name: for key in bucket.list(): # 必須將bucket里清空后,才能刪除掉對(duì)應(yīng)的bucket bucket.delete_key(key.name) conn.delete_bucket(bucket.name) break# 存儲(chǔ)文件流或字符串中的數(shù)據(jù)key = Key(’hello.txt’)key.set_contents_from_file(’/tmp/hello.txt’)
使用boto進(jìn)行https的連接失敗, validate_certs設(shè)置成True或False沒有任何作用
is_secure為Ture時(shí),遇到的報(bào)錯(cuò)如下
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)
is_secure為False時(shí),遇到的報(bào)錯(cuò)如下
http.client.RemoteDisconnected: Remote end closed connection without response
遂更換了botot3
boto3,下面的示例是用的https的(boto對(duì)于https的連接不上,可能是因?yàn)槲业淖C書是自制的,所以才找了這個(gè)包)
import urllib3import boto3urllib3.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 # 將實(shí)際轉(zhuǎn)為本地時(shí)間 print({'name': bucket.name, 'create_date': datetime.datetime.strftime(bucket.creation_date + datetime.timedelta(hours=8), '%Y-%m-%d %H:%M:%S')})# 刪除指定的bucketfor bucket in s3.buckets.all(): if bucket.name == str_bucket_name: bucket.objects.all().delete() # 等價(jià)于下面兩行 # for obj in bucket.objects.all(): # obj.delete() bucket.delete()# 存儲(chǔ)文件流或字符串中的數(shù)據(jù)s3.Object(’mybucket’, ’hello.txt’).put(Body=open(’/tmp/hello.txt’, ’rb’))
以上就是python boto和boto3操作bucket的示例的詳細(xì)內(nèi)容,更多關(guān)于python 操作bucket的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. JavaScript中常見的幾種獲取元素的方式2. Xml簡(jiǎn)介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理3. phpstudy apache開啟ssi使用詳解4. ASP.NET MVC使用異步Action的方法5. ajax實(shí)現(xiàn)頁面的局部加載6. jsp實(shí)現(xiàn)登錄驗(yàn)證的過濾器7. jsp文件下載功能實(shí)現(xiàn)代碼8. uni-app結(jié)合.NET 7實(shí)現(xiàn)微信小程序訂閱消息推送9. 爬取今日頭條Ajax請(qǐng)求10. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算
