Interact with Stacks using the S3 API endpoint

Updated 2 months ago

This page shows how to interact with Stacks using the S3 API endpoint in Python and Node.js to upload and process your files.

Python

The snippet below demonstrates how to instantiate a Boto client to interact with your Koyeb Stacks. Once the boto3 client configured, you can use the Boto SDK as usual. All operations performed will be performed on your Koyeb Stacks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import boto3
from botocore.exceptions import ClientError

s3_client = boto3.client(
    's3',
    aws_access_key_id=KOYEB_STACK_ACCESS_KEY,
    aws_secret_access_key=KOYEB_STACK_SECRET_KEY,
    endpoint_url='https://eu-west-1.stacks.prod.koyeb.com'
    region='eu-west-1',
)

def upload_file(file_name, stack_name, object_name=None):
    if object_name is None:
        object_name = file_name
    try:
        response = s3_client.upload_file(file_name, stack_name, object_name)
    except ClientError as e:
        logging.error(e)
        return False
    return True

Node.js

The snippet below demonstrates how to instantiate the AWS JS SDK client to interact with your Koyeb Stacks. Once the AWS JS SDK client configured, you can use the SDK methods as usual. All operations performed will be performed on your Koyeb Stacks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
const AWS = require('aws-sdk');

const s3 = new AWS.S3({
  endpoint: 'https://eu-west-1.stacks.prod.koyeb.com',
  accessKeyId: KOYEB_STACK_ACCESS_KEY,
  secretAccessKey: KOYEB_STACK_SECRET_KEY,
  region: 'eu-west-1'
})

const uploadFile = (fileName, stackName, objectName) => {
    const fileContent = fs.readFileSync(fileName);

    if (objectName === null) {
        objectName = fileName
    }

    const params = {
        Bucket: stackName,
        Key: objectName,
        Body: fileContent
    };

    s3.upload(params, function(err, data) {
        if (err) {
            throw err;
        }

        console.log(`File uploaded successfully. ${data.Location}`);
    });
};

footer-frame