Skip to content

Commit

Permalink
feat: delete object api
Browse files Browse the repository at this point in the history
  • Loading branch information
abose committed Apr 8, 2023
1 parent 854ec74 commit 6608bb0
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 23 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
48 changes: 36 additions & 12 deletions src/aws_s3_client_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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}`);
}
Expand All @@ -181,5 +204,6 @@ export default {
uploadFileToBucket,
getObject,
downloadObject,
listObjects
listObjects,
deleteObject
};
5 changes: 3 additions & 2 deletions src/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
*/
32 changes: 26 additions & 6 deletions src/wasabi_storage_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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
};

0 comments on commit 6608bb0

Please sign in to comment.