Skip to main content

snippets_js

Integration

New integration from credential
const awsS3Client = yepcode.integration.awsS3("credential-slug");
New integration from plain authentication data
const { S3Client } = require("@aws-sdk/client-s3");

const awsS3Client = new S3Client({
credentials: {
accessKeyId: "accessKeyId",
secretAccessKey: "secretAccessKey",
},
});

List Buckets

List buckets
const { ListBucketsCommand } = require("@aws-sdk/client-s3");

const listBucketsCommand = new ListBucketsCommand({})

const listBucketsResult = await awsS3Client.send(listBucketsCommand);
listBucketsResult.Buckets.forEach(console.log)

Create a Bucket

Create a bucket
const { CreateBucketCommand } = require("@aws-sdk/client-s3");

const createBucketCommand = new CreateBucketCommand({ Bucket: "bucket-name" });

awsS3Client.send(createBucketCommand).then(console.log).catch(console.error);

Delete a Bucket

Delete a bucket
const { DeleteBucketCommand } = require("@aws-sdk/client-s3");

const deleteBucketCommand = new DeleteBucketCommand({ Bucket: "bucket-name" });

awsS3Client.send(deleteBucketCommand).then(console.log).catch(console.error);

Get File Content

Get file content
const { GetObjectCommand } = require("@aws-sdk/client-s3");

const getObjectCommand = new GetObjectCommand({
Bucket: "bucket-name",
Key: "object-name",
});

const getObjectResult = await awsS3Client.send(getObjectCommand);

// Result.Body is a stream of object content
getObjectResult.Body.on("data", (data) => console.log(data.toString("utf8")));

Upload File

Upload file
const { PutObjectCommand } = require("@aws-sdk/client-s3");

const putObjectCommand = new PutObjectCommand({
Body: "Some string body, or stream with defined length",
Bucket: "bucket-name",
Key: "object-name",
});

awsS3Client.send(putObjectCommand).then(console.log).catch(console.error);

Upload File with Stream

Upload file with stream
const { Upload } = require("@aws-sdk/lib-storage");

const upload = new Upload({
client: awsS3Client,
params: {
Bucket: "bucket-name",
Key: "object-name",
Body: readableStream,
},
});

upload.done().then(console.log).catch(console.error);