diff --git a/package-lock.json b/package-lock.json index 5b55cff..47d8bc8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@aicore/wasabi-storage-lib", - "version": "1.0.2", + "version": "1.0.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@aicore/wasabi-storage-lib", - "version": "1.0.2", + "version": "1.0.3", "license": "AGPL-3.0-or-later", "dependencies": { "aws-sdk": "2.1353.0", diff --git a/package.json b/package.json index 4509f91..d8b7cdd 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@aicore/wasabi-storage-lib", - "version": "1.0.2", + "version": "1.0.3", "description": "Template for nodejs with unit gulp build, test, coverage, code guardian, github and Other defaults", "main": "src/wasabi_storage_module.js", "type": "module", diff --git a/src/aws_s3_client_module.js b/src/aws_s3_client_module.js index cc269a1..06ac079 100644 --- a/src/aws_s3_client_module.js +++ b/src/aws_s3_client_module.js @@ -98,6 +98,37 @@ async function getObject(accessKeyId, secretAccessKey, region, bucketName, objec } } +/** + * deletes the given object. + * + * @param accessKeyId bucket specific unique identifier required for authentication + * @param secretAccessKey user specific unique identifier required for authentication + * @param region indicates the geographical server location (e.g us-east-1, eu-west-1a) + * @param bucketName uniquely identifies the bucket where the file should be uploaded + * @param objectName object to be retrieved is passed on as a parameter + * @param url suffix url to decide whether to upload the file to AWS S3 or LiNode Object Storage + * @returns getObjectResponse + */ +async function deleteObject(accessKeyId, secretAccessKey, region, bucketName, objectName, url) { + try { + const s3Client = new S3({ + accessKeyId: accessKeyId, + secretAccessKey: secretAccessKey, + endpoint: new Endpoint('https://s3.'+ region + url + ) + }); + + let params = { + Bucket: bucketName, + Key: objectName + }; + + return await s3Client.deleteObject(params).promise(); + } catch (e) { + throw new Error(`Could not retrieve file from bucket: ${e.message}`); + } +} + /** * Module to get the object data. The path of the file to be retrieved is * passed on as a parameter and the obejct stream is fetched using AWS S3 client. @@ -143,12 +174,12 @@ function downloadObject(accessKeyId, secretAccessKey, region, bucketName, object * @param secretAccessKey user specific unique identifier required for authentication * @param region indicates the geographical server location (e.g us-east-1, eu-west-1a) * @param bucketName uniquely identifies the bucket where the file should be uploaded - * @param url suffix url to decide whether to upload the file to AWS S3 or LiNode Object Storage * @param prefix a string to narrow down to specific objects. Eg, to return all files in dir `a/b/`, * pass in prefix as `a/b/` + * @param url suffix url to decide whether to upload the file to AWS S3 or LiNode Object Storage * @returns listObjectResponse */ -function listObjects(accessKeyId, secretAccessKey, region, bucketName, url, prefix) { +function listObjects(accessKeyId, secretAccessKey, region, bucketName, prefix, url) { try { const s3Client = new S3({ accessKeyId: accessKeyId, @@ -162,15 +193,7 @@ function listObjects(accessKeyId, secretAccessKey, region, bucketName, url, pref Prefix: prefix }; - return new Promise((resolve, reject)=>{ - s3Client.listObjects(params, function (err, data) { - if(err) { - reject(err); - return; - } - resolve(data); - }); - }); + return s3Client.listObjects(params).promise(); } catch (e) { throw new Error(`Could not listObjects bucket: ${e.message}`); } @@ -181,5 +204,6 @@ export default { uploadFileToBucket, getObject, downloadObject, - listObjects + listObjects, + deleteObject }; diff --git a/src/driver.js b/src/driver.js index efad381..ee4805c 100644 --- a/src/driver.js +++ b/src/driver.js @@ -6,11 +6,12 @@ const secretKey = ''; const region = ''; const bucketName = ''; async function run() { + console.log(await wasabi.listObjects(accessKey, secretKey, region, bucketName, "dirInBucket/")); //const response = await wasabi.uploadFileToBucket(accessKey, secretKey, region, fileName, bucketName); // const fileUrl = await wasabi.fetchObject(accessKey, secretKey, region, bucketName, fileName); // await wasabi.downloadFileFromBucket(accessKey, secretKey, region, bucketName, fileName, "a.tar.gz"); - console.log(await wasabi.listObjects(accessKey, secretKey, region, bucketName, "testApp/")); - console.log("done"); + // console.log(await wasabi.deleteObject(accessKey, secretKey, region, bucketName, fileName)); + // console.log("done"); } run().catch(console.log); */ \ No newline at end of file diff --git a/src/wasabi_storage_module.js b/src/wasabi_storage_module.js index 29e64c0..51d8de5 100644 --- a/src/wasabi_storage_module.js +++ b/src/wasabi_storage_module.js @@ -17,7 +17,7 @@ */ import s3Client from './aws_s3_client_module.js'; -const BASE_LINODE_URL_SUFFIX = '.wasabisys.com'; +const BASE_WASABI_URL_SUFFIX = '.wasabisys.com'; /** * Wasabi Storage Helper Module to upload a file to object storage. The file is uploaded using @@ -39,7 +39,7 @@ const BASE_LINODE_URL_SUFFIX = '.wasabisys.com'; } const response = await s3Client.uploadFileToBucket(accessKeyId, - secretAccessKey, region, file, bucket, BASE_LINODE_URL_SUFFIX, objectNameOverride); + secretAccessKey, region, file, bucket, BASE_WASABI_URL_SUFFIX, objectNameOverride); console.log("Object Upload Response : " + JSON.stringify(response)); return response; } @@ -61,7 +61,7 @@ async function fetchObject(accessKeyId, secretAccessKey, region, bucketName, obj "and bucketName are required parameters"); } const response = await s3Client.getObject(accessKeyId, secretAccessKey, region, - bucketName, objectName, BASE_LINODE_URL_SUFFIX); + bucketName, objectName, BASE_WASABI_URL_SUFFIX); if (!response || !response.Body || !response.Body.data) { throw new Error("Invalid Response: Body or Body.Data is missing"); @@ -89,7 +89,7 @@ async function downloadFileFromBucket(accessKeyId, secretAccessKey, region, buck "and bucketName are required parameters"); } return s3Client.downloadObject(accessKeyId, secretAccessKey, region, - bucketName, objectName, BASE_LINODE_URL_SUFFIX, localPath); + bucketName, objectName, BASE_WASABI_URL_SUFFIX, localPath); } /** @@ -110,12 +110,32 @@ async function listObjects(accessKeyId, secretAccessKey, region, bucketName, pre "and bucketName, prefix are required parameters"); } return s3Client.listObjects(accessKeyId, secretAccessKey, region, - bucketName, BASE_LINODE_URL_SUFFIX, prefix); + bucketName, prefix, BASE_WASABI_URL_SUFFIX); +} + +/** + * deletes a given object from bucket + * + * @param accessKeyId bucket specific unique identifier required for authentication + * @param secretAccessKey user specific unique identifier required for authentication + * @param region indicates the geographical server location (e.g us-east-1, eu-west-1a) + * @param bucketName uniquely identifies the bucket where the file should be uploaded + * @param objectName object to be retrieved is passed on as a parameter + * @returns deleteObjectResponse + */ +async function deleteObject(accessKeyId, secretAccessKey, region, bucketName, objectName) { + if (!region || !bucketName || !objectName) { + throw new Error("Invalid parameter value: accessToken, region, fileName " + + "and bucketName, prefix are required parameters"); + } + return s3Client.deleteObject(accessKeyId, secretAccessKey, region, + bucketName, objectName, BASE_WASABI_URL_SUFFIX); } export default { uploadFileToBucket, downloadFileFromBucket, fetchObject, - listObjects + listObjects, + deleteObject };