Angular 12 Multiple Image Upload To S3 using DropZone & AWS-SDK
We are going to use Angular 12 with cool packages like dropzone and aws-sdk for uploading images with preview to AWS S3 bucket π. Beautiful part π is we not only focus π on integrating packages but also fixing errors that might occur on our journey. πͺ
Walk through all steps from Zero to Hero π₯
Steps include are:
- Setup Angular-12 π₯
- Install ngx-dropzone and ngx-toastr
- Add aws-sdk package and play with some coding π
- Deals with configuration issues π
- Time to Design UI π
- Setup S3 bucket
- Add CORS permission
- Create IAM user to be allowed to upload π
- Celebrate π₯
- GitHub link π
1. Setup Angular-12 π₯
using angular-cli, just place the following command inside cmd and hit enter
ng new s3-img-upload
2. Install ngx-dropzone and ngx-toastr
In order to create image uploader zone and for notification messages we will be using ngx-toastr
npm i ngx-dropzonenpm i ngx-toastr
To configure ngx-dropzone we place the following code to working module in our case it's app.module.ts
// in app.module.ts
import { NgxDropzoneModule } from 'ngx-dropzone';
@NgModule({
...
imports: [
NgxDropzoneModule
],
...
})
export class AppModule { }
To configure ngx-toastr, involves two steps:
Step 1: Add style path to angular.json file
"styles": [ "src/styles.css", "node_modules/ngx-toastr/toastr.css"],
Step 2: Toastr configuration for App.module.ts looks like
import { CommonModule } from '@angular/common';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { ToastrModule } from 'ngx-toastr';
@NgModule({
imports: [
CommonModule,
BrowserAnimationsModule, // required animations module
ToastrModule.forRoot(), // ToastrModule added
],
bootstrap: [App],
declarations: [App],
})
class MainModule {}
3. Add aws-sdk package and play with some coding π
Now simple install aws-sdk package as
npm i aws-sdk
wow, cool, we are getting close β€οΈ
letβs create a service that will communicate with our aws-s3 bucket ποΈ for uploading images β‘
Note: For Learnings purpose, Iβm leaving my credentials as it is. Surely this will not work for your case because I will cleanup these secrets .π
Hold your houses π, on later section I will let u know to create these credentials that are:
- ACCESS_KEY_ID
- SECRET_ACCESS_KEY
- BUCKET_REGION
- BUCKET_NAME
Time β to import the service our component as:
export class AppComponent {constructor(private uploadS3Service: UploadS3Service) {}}
Letβs build and run. Oh, No! π we are trapped with build errors.
4. Deals with configuration issues π
Donβt worry. Letβs fix them one by one
Solution:
Step 1: Install the package
npm install β save-dev @types/node
Step 2: Add βtypesβ: [βnodeβ] to tsconfig.app.json file as:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": [
"node"
]},
"files": [
"src/main.ts",
"src/polyfills.ts"
],
"include": [
"src/**/*.d.ts"
]
}
Step 3: Add the following lines to polyfill.ts
if (typeof (window as any).global === 'undefined') {
(window as any).global = window;
}
In some angular version, above solution works π, but if you're still facing console errors like:
Install another package
npm install -S util
Let restart the server, Yaaahh! Its works π₯ π
5. Time to Design UI π
Continue with coding, move to app.component.ts and start implementing the different function according to user actions:
onSelect : To be called once user select an image.
onRemove: Remove the image from ngx-dropzone, once user click on cross icon on image
onImageUpdate: Finally the submit function
so finally app.component.ts
and app.component.html
And app.component.css
So finally Our Design Looks like:
6. Setup S3 bucket
Go to your AWS account and create S3 bucket by specifying the region closer to your location
Place S3 bucket name, lowercase should be unique globally
So here we got
BUCKET_REGION = βus-east-2βBUCKET_NAME = βpurpleappfood123β
Wow ! Bucket has been created successfully!
Important : in order to allow image upload, we must set CORS permission
7. Add CORS Permission
So goes to permission Tab here and move down. At the end you will find the βCross-origin resource sharing (CORS)β section.
Place the following permissions and hit enter
8. Create IAM user to be allowed to upload π
Be calm, we are just at the end. We need a IAM user that have permission to be uploaded to S3. So we will be gonna that user permission on our project
Important: Assigning full permission is really a security secret, you can go into more granular level
So finally we Get the permission as, which will be used on angular service for image upload
ACCESS_KEY_ID ="AKIASNQ57G3KG34NQ7O6"
SECRET_ACCESS_KEY ="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"
9. Celebrate π₯
Wow, we have arrived at our destination. Multiple images are being upload to S3. Toastr message pops up on top right corner π₯
10. GitHub link π
Thank you so much π for being with me through the whole journey.