Created
August 25, 2023 13:30
-
-
Save bmatusiak/75d3024c6411160e5c0227da2e3ef30a to your computer and use it in GitHub Desktop.
s3 js-v3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const s3_bucket = process.env.AWS_BUCKET; | |
const s3_region = process.env.AWS_REGION; | |
(async function () { | |
const { | |
S3Client, | |
ListObjectsCommand, | |
GetObjectCommand, | |
PutObjectCommand, | |
DeleteObjectCommand | |
} = require("@aws-sdk/client-s3"); | |
const client = new S3Client({ | |
AWS_REGION: s3_region | |
}); | |
async function listData(callback) { | |
return new Promise(function (resolve, reject) { | |
const command = new ListObjectsCommand({ Bucket: s3_bucket }); | |
try { | |
client.send(command).then(function (data) { | |
var keys = data.Contents.map((d) => d.Key); | |
if (callback) callback(keys) | |
resolve(keys) | |
}) | |
} catch (error) { | |
reject(error) | |
} | |
}); | |
} | |
async function setData(key, value, callback) { | |
return new Promise(function (resolve, reject) { | |
const command = new PutObjectCommand({ Bucket: s3_bucket, Key: key, Body: value }); | |
try { | |
client.send(command).then(() => { | |
if (callback) | |
callback("success"); | |
resolve("success") | |
}); | |
} catch (error) { | |
reject(error) | |
} | |
}); | |
} | |
async function getData(key, callback) { | |
var body = ""; | |
return new Promise(function (resolve, reject) { | |
const command = new GetObjectCommand({ Bucket: s3_bucket, Key: key }); | |
try { | |
client.send(command).then(function (data) { | |
if (data.Body && callback) { | |
data.Body.on('readable', function () { | |
var s = data.Body.read(); | |
if (s) | |
body += s; | |
}); | |
data.Body.on('end', function () { | |
callback(body) | |
resolve(body) | |
}) | |
} | |
}); | |
} catch (error) { | |
reject(error) | |
} | |
}); | |
} | |
async function rmData(key, callback) { | |
return new Promise(function (resolve, reject) { | |
const command = new DeleteObjectCommand({ Bucket: s3_bucket, Key: key }); | |
try { | |
client.send(command).then(() => { | |
if (callback) | |
callback("success"); | |
resolve("success") | |
}); | |
} catch (error) { | |
reject(error) | |
} | |
}); | |
} | |
await listData((list) => console.log(list)); | |
await setData("test-key", "test-value", (body) => console.log(body)); | |
await getData("test-key", (body) => console.log(body)); | |
await listData((list) => console.log(list)); | |
await rmData("test-key", (body) => console.log(body)); | |
await listData((msg) => console.log(msg)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment