python boto和boto3操作bucket的示例
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) # 創(chuàng)建bucket for 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))}) # 刪除指定的bucket for 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 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 # 將實(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")}) # 刪除指定的bucket for 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)注腳本之家其它相關(guān)文章!
相關(guān)文章
tensorflow使用CNN分析mnist手寫體數(shù)字?jǐn)?shù)據(jù)集
這篇文章主要介紹了tensorflow使用CNN分析mnist手寫體數(shù)字?jǐn)?shù)據(jù)集,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06Python datetime 如何處理時(shí)區(qū)信息
這篇文章主要介紹了Python datetime 如何處理時(shí)區(qū)信息,幫助大家更好的用python 處理時(shí)間,感興趣的朋友可以了解下。2020-09-09Python微服務(wù)開發(fā)之使用FastAPI構(gòu)建高效API
微服務(wù)架構(gòu)在現(xiàn)代軟件開發(fā)中日益普及,它將復(fù)雜的應(yīng)用程序拆分成多個(gè)可獨(dú)立部署的小型服務(wù)。本文將介紹如何使用 Python 的 FastAPI 庫快速構(gòu)建和部署微服務(wù),感興趣的可以了解一下2023-05-05TensorFlow MNIST手寫數(shù)據(jù)集的實(shí)現(xiàn)方法
MNIST數(shù)據(jù)集中包含了各種各樣的手寫數(shù)字圖片,這篇文章主要介紹了TensorFlow MNIST手寫數(shù)據(jù)集的實(shí)現(xiàn)方法,需要的朋友可以參考下2020-02-02gethostbyaddr在Python3中引發(fā)UnicodeDecodeError
本文介紹了gethostbyaddr()在Python?3中引發(fā)UnicodeDecodeError的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2022-05-05Python字典生成式、集合生成式、生成器用法實(shí)例分析
這篇文章主要介紹了Python字典生成式、集合生成式、生成器用法,結(jié)合實(shí)例形式分析了Python字典生成式、集合生成式、生成器相關(guān)原理、使用技巧與操作注意事項(xiàng),需要的朋友可以參考下2020-01-01python unichr函數(shù)知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是一篇關(guān)于python unichr函數(shù)的知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解
這篇文章主要介紹了Python數(shù)據(jù)可視化圖實(shí)現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06