|
| 1 | +# Import-Service |
| 2 | + |
| 3 | +1. Install the latest version of AWS SDK |
| 4 | + |
| 5 | +Run the following command to create a default package.json file: |
| 6 | + |
| 7 | +```sh |
| 8 | +`npm init -y` |
| 9 | +``` |
| 10 | + |
| 11 | +Run the following command to install the Amazon S3 client package: |
| 12 | + |
| 13 | +```sh |
| 14 | +`npm i @aws-sdk/client-s3` |
| 15 | +``` |
| 16 | + |
| 17 | +Add the following code to a file named index.js in the yours folder: |
| 18 | + |
| 19 | +```js |
| 20 | +// This is used for getting user input. |
| 21 | +import { createInterface } from "readline/promises"; |
| 22 | + |
| 23 | +import { |
| 24 | + S3Client, |
| 25 | + PutObjectCommand, |
| 26 | + CreateBucketCommand, |
| 27 | + DeleteObjectCommand, |
| 28 | + DeleteBucketCommand, |
| 29 | + paginateListObjectsV2, |
| 30 | + GetObjectCommand, |
| 31 | +} from "@aws-sdk/client-s3"; |
| 32 | + |
| 33 | +export async function main() { |
| 34 | + // A region and credentials can be declared explicitly. For example |
| 35 | + // `new S3Client({ region: 'us-east-1', credentials: {...} })` would |
| 36 | + //initialize the client with those settings. However, the SDK will |
| 37 | + // use your local configuration and credentials if those properties |
| 38 | + // are not defined here. |
| 39 | + const s3Client = new S3Client({}); |
| 40 | + |
| 41 | + // Create an Amazon S3 bucket. The epoch timestamp is appended |
| 42 | + // to the name to make it unique. |
| 43 | + const bucketName = `test-bucket-${Date.now()}`; |
| 44 | + await s3Client.send( |
| 45 | + new CreateBucketCommand({ |
| 46 | + Bucket: bucketName, |
| 47 | + }) |
| 48 | + ); |
| 49 | + |
| 50 | + // Put an object into an Amazon S3 bucket. |
| 51 | + await s3Client.send( |
| 52 | + new PutObjectCommand({ |
| 53 | + Bucket: bucketName, |
| 54 | + Key: "my-first-object.txt", |
| 55 | + Body: "Hello JavaScript SDK!", |
| 56 | + }) |
| 57 | + ); |
| 58 | + |
| 59 | + // Read the object. |
| 60 | + const { Body } = await s3Client.send( |
| 61 | + new GetObjectCommand({ |
| 62 | + Bucket: bucketName, |
| 63 | + Key: "my-first-object.txt", |
| 64 | + }) |
| 65 | + ); |
| 66 | + |
| 67 | + console.log(await Body.transformToString()); |
| 68 | + |
| 69 | + // Confirm resource deletion. |
| 70 | + const prompt = createInterface({ |
| 71 | + input: process.stdin, |
| 72 | + output: process.stdout, |
| 73 | + }); |
| 74 | + |
| 75 | + const result = await prompt.question("Empty and delete bucket? (y/n) "); |
| 76 | + prompt.close(); |
| 77 | + |
| 78 | + if (result === "y") { |
| 79 | + // Create an async iterator over lists of objects in a bucket. |
| 80 | + const paginator = paginateListObjectsV2( |
| 81 | + { client: s3Client }, |
| 82 | + { Bucket: bucketName } |
| 83 | + ); |
| 84 | + for await (const page of paginator) { |
| 85 | + const objects = page.Contents; |
| 86 | + if (objects) { |
| 87 | + // For every object in each page, delete it. |
| 88 | + for (const object of objects) { |
| 89 | + await s3Client.send( |
| 90 | + new DeleteObjectCommand({ Bucket: bucketName, Key: object.Key }) |
| 91 | + ); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + // Once all the objects are gone, the bucket can be deleted. |
| 97 | + await s3Client.send(new DeleteBucketCommand({ Bucket: bucketName })); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// Call a function if this file was run directly. This allows the file |
| 102 | +// to be runnable without running on import. |
| 103 | +import { fileURLToPath } from "url"; |
| 104 | +if (process.argv[1] === fileURLToPath(import.meta.url)) { |
| 105 | + main(); |
| 106 | +} |
| 107 | +``` |
| 108 | +
|
| 109 | +2. Install the CSV parser package: |
| 110 | +
|
| 111 | +```sh |
| 112 | +`npm i csv-parser` |
| 113 | +``` |
| 114 | +
|
| 115 | +# Task 7 |
| 116 | +https://github.com/rolling-scopes-school/aws/blob/main/aws-developer/07_authorization/task.md |
| 117 | +
|
| 118 | +### Links |
| 119 | +- [Product Service API](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products) |
| 120 | +- [Product Service API for id = 8f79f8e2-8841-4dc3-871e-2f7c861b130e](https://dapdmi8g8h.execute-api.us-east-1.amazonaws.com/prod/products/8f79f8e2-8841-4dc3-871e-2f7c861b130e) |
| 121 | +- [Frontend](https://d3oeh93tzbcw2m.cloudfront.net/) |
| 122 | +
|
| 123 | +### Settings: |
| 124 | +[Token Generator](https://www.debugbear.com/basic-auth-header-generator) |
| 125 | +userName = 'Tati-Moon' |
| 126 | +password = |
| 127 | +> add to console localStorage.setItem('authorization_token', "SOME_VALID_TOKEN"); |
| 128 | +or |
| 129 | +> add to console localStorage.setItem('authorization_token', "SOME_INVALID_TOKEN"); |
| 130 | +
|
| 131 | +### CVS: |
| 132 | +title,description,price,count |
| 133 | +some,some,666,666 |
| 134 | +or |
| 135 | +title,description,price,count,photo |
| 136 | +Product x, Product x,111,9,https://d1yjjnpx0p53s8.cloudfront.net/styles/logo-thumbnail/s3/0025/1559/brand.gif?itok=vXujPldk |
| 137 | +
|
| 138 | +# Evaluation Criteria |
| 139 | +- [x] 100/100 |
| 140 | +
|
| 141 | +- [x] ### Task 7.1 |
| 142 | +1. Created a new service called `authorization-service` |
| 143 | +2. Created a lambda function called `basicAuthorizer` under the Authorization Service |
| 144 | +3. Lambda have environment variables: `GITHUB_LOGIN` and `GITHUB_PASSWORD` |
| 145 | +
|
| 146 | + |
| 147 | +
|
| 148 | +- [x] ### Task 7.2 |
| 149 | +1. Added Lambda authorization to the /import path of the Import Service API Gateway. |
| 150 | +2. Used basicAuthorizer lambda as the Lambda authorizer |
| 151 | +
|
| 152 | +- [x] ### Task 7.3 |
| 153 | +1. The request from the client application to the /import path of the Import Service was required to have a Basic Authorization header: |
| 154 | +Authorization: Basic {authorization_token} |
| 155 | +
|
| 156 | +2. The {authorization_token} was a base64-encoded {your_github_account_login} |
| 157 | +example: Authorization: Basic sGLzdRxvZmw0ZXs0UGFzcw== |
| 158 | +
|
| 159 | +3. The client obtained the authorization_token value from the browser's localStorage: |
| 160 | +const authorization_token = localStorage.getItem('authorization_token') |
| 161 | +
|
| 162 | + |
| 163 | +
|
| 164 | +- [x] ### Additional |
| 165 | +
|
| 166 | +The client application displayed alerts for responses with 401 and 403 HTTP statuses. This behavior was added to the nodejs-aws-fe-main/src/index.tsx file. |
0 commit comments