Latest news:
Sahih al-Bukhari (সহীহ বুখারী) is a free Hadith application for android. This application is advertisement free. Download now https://play.google.com/store/apps/details?id=com.akramhossin.bukharisharif
In this blog post i will explain how to upload and delete file in AWS S3 using aws-sdk.
To getting started we need to install aws-sdk npm package to our Node.js application. To install aws-sdk in any Node.js project
run the following command in your terminal
$ npm install aws-sdk
After successful installation of aws-sdk package. You need to provide credentials to AWS so that only your account and its resources
are accessed by the SDK. So far we need to following information to configure AWS S3.
Now lets get started. We will create new Typescript Class as S3Helper. Reason is we can easily access this class from any where
in our Node.js application.
at first we will create some public variable into our S3Helper class.
accessKeyId: string;
secretAccessKey: string;
bucketName: string;
s3: any;
region: string;
Now lets complete constructor part
constructor() {
this.accessKeyId = 'YourAccessKeyId ';
this.secretAccessKey = 'YourSecretAccessKey ';
this.bucketName = 'YourS3BucketName';
this.region = 'YourS3RegionName';
this.s3 = new aws.S3({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey
})
}
We successfully initialize our S3Helper class constructor. In that s3 holds the objects from aws-sdk S3 class.
We can reuse that object any where in this class file.
Now we will proceed to next part. Which is upload object to AWS S3 .
async uploadFile(objectKey: any, objectPath: any) {
const directoryPath = path.join(__dirname, '../../', objectPath);
const fileContent = fs.readFileSync(directoryPath);
try {
const params = {
Bucket: this.bucketName,
Key: objectKey,
Body: fileContent,
ContentType: mime.lookup(directoryPath)
};
const result = await this.s3.putObject(params).promise();
console.log({
file:`https://` + this.bucketName + `.s3-` + this.region + `.amazonaws.com/` + objectKey,
//params:params
});
} catch (error) {
console.log(error);
}
};
uploadFile function accepts two parameters, one is object key name and another one is object path. this path is actuall location of
the file. AWS S3 require Content type of the file, for getting content type of the file we can use mime-types npm package into our
Node.js application. To install run this command into your terminal
$ npm install mime-types
We are completed the upload file part for AWS S3.
Now lets implement delete file part. We will use following code to delete file from AWS S3
async deleteFile(objectKey: any) {
try {
const params = {
Bucket: this.bucketName,
Key: objectKey,
};
const result = await this.s3.deleteObject(params).promise();
console.log({
file:`https://` + this.bucketName + `.s3-` + this.region + `.amazonaws.com/` + objectKey,
//params:params
});
} catch (error) {
console.log(error);
}
}
deleteFile function accept one parameter as objectKey, which is exact same name of the uploaded file in S3 bucket.
complete code of S3Helper class will be like this
export class S3Helper {
accessKeyId: string;
secretAccessKey: string;
bucketName: string;
s3: any;
region: string;
constructor() {
this.accessKeyId = param.s3AccessKeyId;
this.secretAccessKey = param.s3SecretAccessKey;
this.bucketName = param.s3BucketName;
this.region = param.s3Region;
this.s3 = new aws.S3({
accessKeyId: this.accessKeyId,
secretAccessKey: this.secretAccessKey
})
}
async uploadFile(objectKey: any, objectPath: any) {
const directoryPath = path.join(__dirname, '../../', objectPath);
const fileContent = fs.readFileSync(directoryPath);
try {
const params = {
Bucket: this.bucketName,
Key: objectKey,
Body: fileContent,
ContentType: mime.lookup(directoryPath)
};
const result = await this.s3.putObject(params).promise();
console.log({
file:`https://` + this.bucketName + `.s3-` + this.region + `.amazonaws.com/` + objectKey,
//params:params
});
} catch (error) {
console.log(error);
}
};
async deleteFile(objectKey: any) {
try {
const params = {
Bucket: this.bucketName,
Key: objectKey,
};
const result = await this.s3.deleteObject(params).promise();
console.log({
file:`https://` + this.bucketName + `.s3-` + this.region + `.amazonaws.com/` + objectKey,
//params:params
});
} catch (error) {
console.log(error);
}
}
}
Usage:
Import the S3Helper class to your desired class file, initialize it by using below code into class constractor
constructor(
private s3Helper: S3Helper,
) { }
To call S3Helper upload and delete method use below code
To Upload:
await this.s3Helper.uploadFile(savedModel.imageName, "uploads/" + savedModel.imageName);
To Delete:
await this.s3Helper.deleteFile(model.backgroundImage);
Happy Coding!
Views : 867