Skip to content
Console

Connect to S4 with Boto3 (Python 3)

Add the boto3 framework

Prerequisite: install the framework (https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html )

s4-demo.py
# import
import boto3
from botocore.client import Config

# Config
s3endpoint = 'https://s3.bitiful.net' # Use the value shown under the "Endpoint" label at the bottom of the "Bucket settings" page in the console
s3region = 'cn-east-1'
s3accessKeyId = 'sub-account accessKey' # Create a sub-account in the console, then create an accessKey for it
s3SecretKeyId = 'sub-account secretKey' # Important: when you create a sub-account, you must manually grant it specific permissions!

Connect to the S4 endpoint

s4-demo.py
    s3client = boto3.client(
        's3',
        aws_access_key_id = s3accessKeyId,
        aws_secret_access_key = s3SecretKeyId,
        endpoint_url = s3endpoint,
        region_name = s3region
     )

List objects in a bucket

s4-demo.py
    buckets = s3client.list_buckets()
    for bucket in buckets['Buckets']:
        print(bucket["Name"])

Full-stack example: use the S3 SDK for Python (Boto3) to issue a presigned PutObject URL for browser JavaScript uploads (a PUT request sent with axios)

s4-demo.py
import boto3
from flask import Flask
from flask import render_template_string
from flask import request

app = Flask(__name__)


@app.get('/upload_url')
def get_upload_url():
    # Config
    s3endpoint = 'https://s3.bitiful.net' # Use the value shown under the "Endpoint" label at the bottom of the "Bucket settings" page in the console
    s3region = 'cn-east-1'
    s3accessKeyId = 'sub-account accessKey' # Create a sub-account in the console, then create an accessKey for it
    s3SecretKeyId = 'sub-account secretKey' # Important: when you create a sub-account, you must manually grant it specific permissions!

    # Connect to S3
    client = boto3.client(
        's3',
        aws_access_key_id = s3accessKeyId,
        aws_secret_access_key = s3SecretKeyId,
        endpoint_url = s3endpoint,
        region_name = s3region
    )
    url = client.generate_presigned_url(
        'put_object',
        Params={
            'Bucket': 'xxxxxx',
            'Key': request.args.get('key'),
        },
        ExpiresIn=3600
    )
    return {'url': url}


@app.get('/')
def index():
    return render_template_string('''
<!DOCTYPE html>
<html>
<head>
  <title>S3 File Upload</title>
</head>
<body>
<div>
  <h1>S3 File Upload</h1>
  <input type="file" id="file">
  <button onclick="handleClick()">Upload</button>
</div>
<script src="https://cdn.bootcdn.net/ajax/libs/axios/1.3.6/axios.min.js"></script>
<script>
    let file;
    let fileContent;

    const fileInput = document.getElementById('file');
    const fileReader = new FileReader();

    fileInput.addEventListener('change', function (event) {
        file = event.target.files[0];
        console.log("file is: ", file);
        fileReader.readAsArrayBuffer(file);
    });

    fileReader.onload = function (event) {
        fileContent = event.target.result;
        // console.log("fileContent is: ", fileContent);
    };

    function handleClick() {
        console.log('click');

        const url = `/upload_url`;
        const params = {key: '1.jpg'};

        axios.get(url, {params: params})
            .then(function (res) {
                console.log(res);
                const url = res.data.url;
                const config = {
                    headers: {
                        'Content-Type': null,
                    },
                };
                axios.put(url, file, config)
                    .then(function (res) {
                        console.log("res is: ", res);
                    });
            });

    }
</script>

</body>
</html>    
''')

if __name__ == '__main__':
    app.run(debug=True)