pipe-s3-api
v0.0.13
Published
S3 API for Pipe
Downloads
9
Readme
PIPE S3 API
To Include PIPE S3 API
const PIPE_S3 = require("pipe-s3-api");`
let s3 = new PIPE_S3({
accessKeyId: HUBCLOUD_API_KEY,
secretAccessKey: PIPE_SEED
});
Right now, three basic functions have been implemented
upload()
getObject()
listObjects()
Example Usage
const { randomBytes } = require('crypto');
const PIPE_S3 = require("pipe-s3-api");
let s3 = new PIPE_S3({
accessKeyId: "tanglehub_jIqWO70qVocBEV6awZX3GehESstQH5a9",
secretAccessKey: "a52fa8bda1caec76458db951b4c0911fb53172044547432d78264a0b4235a58b",
});
async function uploadFile(bucketName, fileName, fileContent) {
const params = {
Bucket: bucketName,
Key: fileName,
Body: fileContent
};
try {
return await s3.upload(params);
} catch (err) {
console.error(`Error uploading file: ${err}`);
}
}
async function downloadFile(bucketName, fileName) {
const params = {
Bucket: bucketName,
Key: fileName
};
try {
return await s3.getObject(params);
} catch (err) {
console.error(`Error downloading file: ${err}`);
}
}
async function listFiles(bucketName) {
const params = {
Bucket: bucketName
};
try {
const response = await s3.listObjects(params);
console.log("Files in bucket");
response.Contents.forEach(file => console.log(file.Key));
} catch (err) {
console.error(`Error listing files: ${err}`);
}
}
async function test() {
const buck = "Tes Bucket Name";
let res = await s3.setBucket(buck); //Required Call
if (!res) {
console.log("Could not initiate pipe");
return
}
let contents = randomBytes(1024);
let fileName = "test.hello";
res = await uploadFile(buck, fileName, contents);
console.log("Upload result: ", res);
console.log("Listing files");
res = await listFiles(buck);
res = await downloadFile(buck, fileName);
console.log("Download result: ", res);
}
test()