如何使用Python boto3库进行bucket操作示例?

2026-05-05 10:431阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计323个文字,预计阅读时间需要2分钟。

如何使用Python boto3库进行bucket操作示例?

pythonimport boto.s3.connectionfrom boto.s3.key import Key

如何使用Python boto3库进行bucket操作示例?

conn=boto.connect_s3(aws_access_key_id='12345', aws_secret_access_key='12345', host='127.0.0.1', port=8080, is_secure=False, calling_format=boto.s3.connection.S3Connection)

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进行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的资料请关注易盾网络其它相关文章!

本文共计323个文字,预计阅读时间需要2分钟。

如何使用Python boto3库进行bucket操作示例?

pythonimport boto.s3.connectionfrom boto.s3.key import Key

如何使用Python boto3库进行bucket操作示例?

conn=boto.connect_s3(aws_access_key_id='12345', aws_secret_access_key='12345', host='127.0.0.1', port=8080, is_secure=False, calling_format=boto.s3.connection.S3Connection)

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进行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的资料请关注易盾网络其它相关文章!