Setup boto3 to connect to custom host, similar to s3cmd

126 Views Asked by At

With s3cmd I am able to connect to a bucket with the following config file:

[default]
access_key = <VARIABLE>
secret_key = <VARIABLE>
host_base = <VARIABLE>
host_bucket = <VARIABLE>
use_https = True

I'd rather build a Python script to connect to a S3 bucket instead of s3cmd and am looking at boto3 right now. However I cannot find a way to define the host_bucket variable. I am currently using the following code:

import boto3
s3 = boto3.resource(
    's3',
    aws_access_key_id=<VARIABLE>,
    aws_secret_access_key=<VARIABLE>,
    endpoint_url=<VARIABLE>
)

for bucket in s3.buckets.all():
    print(bucket, bucket.name)

This returns zero.

1

There are 1 best solutions below

0
GuiEpi On

With your CMD use aws configure then you need to fill in the infos.

after in your code use

import boto3
 
# Let's use Amazon S3
s3 = boto3.resource('s3')

and something like this:

for obj in s3.Bucket("mybucket"):
    print(obj.key)

see the documentation