aws s3 python3 boto3 对接易华录的蓝光测试

 通过ssl https ak sk 对接 获取桶名称和上传文件1.txt

第一步 使用boto3.resource 创建连接

第二步列出桶名称s3.buckets.all()

第三步上传文件s3.meta.client.upload_file

import urllib3
import boto3
import datetime
urllib3.disable_warnings()

s3 = boto3.resource(
  service_name='s3',
  aws_access_key_id="xxxxxxxxxxxxxxxx",
  aws_secret_access_key="oooooooooooooooooooooF",
  endpoint_url='https://172.20.2.2:9000',
  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'))

LOCAL_FILE = '1.txt'
BUCKET = 'mybucket'

# Upload file to Y/M/D/random-id/filename
#s3_client = boto3.client('s3', region_name='ap-southeast-2')
path = datetime.datetime.now().strftime("%Y/%m/%d/")  + '/'
print(path)
response = s3.meta.client.upload_file(LOCAL_FILE, BUCKET, path + LOCAL_FILE)

你可能感兴趣的:(三块钱的博客,aws,s3,boto3)