Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rm methods #8

Merged
merged 17 commits into from
Jan 9, 2023
Merged
Next Next commit
fix(dir methods): rm
  • Loading branch information
korostelevm committed Jan 9, 2023
commit be2a2a1884815a8bf256b93d0b04d89c187939ea
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ Require in the same format as Node.js `fs`, specifying an S3 Bucket:
- [ ] fs.rmdir(path, callback)
- [ ] fs.rm(path, callback)
- [ ] fs.unlink(path, callback)
- [ ] fs.lstat(path, callback)
- [ ] fs.createReadStream(path, [options])
- [ ] fs.createWriteStream(path, [options])

Expand Down
38 changes: 35 additions & 3 deletions src/CyclicS3FSPromises.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ const {
PutObjectCommand,
HeadObjectCommand,
ListObjectsCommand,
ListObjectsV2Command
ListObjectsV2Command,
DeleteObjectCommand
} = require("@aws-sdk/client-s3");
const _path = require('path')
const {Stats} = require('fs')
Expand Down Expand Up @@ -68,9 +69,10 @@ class CyclicS3FSPromises{
}

async stat(fileName, data, options={}){
fileName = util.normalize_path(fileName)
const cmd = new HeadObjectCommand({
Bucket: this.bucket,
Key: util.normalize_path(fileName)
Key: fileName
})
let result;
try{
Expand Down Expand Up @@ -121,7 +123,7 @@ class CyclicS3FSPromises{

async readdir(path){
path = util.normalize_dir(path)
const cmd = new ListObjectsCommand({
const cmd = new ListObjectsV2Command({
Bucket: this.bucket,
// StartAfter: path,
Prefix: path,
Expand Down Expand Up @@ -153,6 +155,36 @@ class CyclicS3FSPromises{
return result
}

async rm(path){
try{
let f = await this.stat(path)
}catch(e){
throw e
}
path = util.normalize_path(path)
const cmd = new DeleteObjectCommand({
Bucket: this.bucket,
Key: path
})
try{
await this.s3.send(cmd)
}catch(e){
console.error(e)
}

}


// async rmdir(path){
// path = util.normalize_dir(path)
// try{
// let await readdir(path)
// // await this.s3.send(cmd)
// }catch(e){
// throw e
// }
// }

}


Expand Down
16 changes: 16 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ class CyclicS3FS extends CyclicS3FSPromises {
})
}

rm(path, callback) {
callback = makeCallback(arguments[arguments.length - 1]);
new Promise(async (resolve,reject)=>{
try{
let res = await super.rm(...arguments)
return resolve(callback(null,res))
}catch(e){
return resolve(callback(e))
}
})
}


readFileSync(fileName) {
return sync_interface.runSync(this,'readFile',[fileName])
Expand Down Expand Up @@ -130,6 +142,10 @@ class CyclicS3FS extends CyclicS3FSPromises {
return sync_interface.runSync(this,'mkdir',[path])
}

rmSync(path) {
return sync_interface.runSync(this,'rm',[path])
}

}


Expand Down