SoFunction
Updated on 2024-10-29

Example of python boto and boto3 manipulating a bucket

boto operation

import datetime

import boto.
from boto. import Key
conn = boto.connect_s3(
  aws_access_key_,
  aws_secret_access_key="123456",
  host="127.0.0.1",
  port=8080,
  is_secure=False,
  calling_format=boto.(),
)

str_bucket_name = "bucket_test"
conn.create_bucket(str_bucket_name) # Create a bucket

for bucket in conn.get_all_buckets(): # Get all the buckets
  # Convert actual to local time
  print({"name": , "create_date": str((bucket.creation_date, "%Y-%m-%dT%H:%M:%S.%fZ") + (hours=8))})


# Delete the specified bucket
for bucket in conn.get_all_buckets():
  if  == str_bucket_name:
    for key in (): # You must clear the bucket before you can delete the corresponding bucket.
      bucket.delete_key()
    conn.delete_bucket()
    break

# Store data in file streams or strings
key = Key('')

key.set_contents_from_file('/tmp/')

Failed https connection using boto, validate_certs set to True or False has no effect

When is_secure is Ture, the following error is encountered

: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1076)

When is_secure is False, the following error is encountered

: Remote end closed connection without response

The botot3 was replaced.

boto3, the following example is using https (boto can't connect for https, probably because my certificate is homemade, that's why I looked for this package)

import urllib3
import boto3

urllib3.disable_warnings()

s3 = (
  service_name='s3',
  aws_access_key_,
  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 (): # Get all the buckets
  # Convert actual to local time
  print({"name": , "create_date": (bucket.creation_date + (hours=8), "%Y-%m-%d %H:%M:%S")})

# Delete the specified bucket
for bucket in ():
  if  == str_bucket_name:
    ().delete()  # Equivalent to the following two lines
    # for obj in ():
    #   ()
    ()

# Store data in file streams or strings
('mybucket', '').put(Body=open('/tmp/', 'rb'))

Above is the details of python boto and boto3 operation bucket example, more information about python operation bucket please pay attention to my other related articles!