Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task5 #4

Merged
merged 10 commits into from Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/import-service/.env
@@ -1,4 +1,4 @@
IMPORT_S3_BUCKET=nodejs-aws-task5-csv
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even this constants could be exposed, better to not commit .env file to a repo

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They have a bit different rules:

Note: .env, .env.development, and .env.production files should be included in your repository as they define defaults. .env*.local should be added to .gitignore, as those files are intended to be ignored. .env.local is where secrets can be stored.

https://www.serverless.com/plugins/serverless-dotenv-plugin

So, I'm storing confidential data in .env.*.local files.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok

IMPORT_S3_PREFIX=uploaded/
IMPORT_S3_REGION=us-east-2
PARSED_S3_PREFIX=parsed/
IMPORT_S3_PARSED_PREFIX=parsed/
IMPORT_S3_UPLOAD_PREFIX=uploaded/
2 changes: 1 addition & 1 deletion packages/import-service/serverless.yml
Expand Up @@ -58,5 +58,5 @@ functions:
bucket: "${env:IMPORT_S3_BUCKET}"
event: "s3:ObjectCreated:*"
rules:
- prefix: "${env:IMPORT_S3_PREFIX}"
- prefix: "${env:IMPORT_S3_UPLOAD_PREFIX}"
existing: true
9 changes: 6 additions & 3 deletions packages/import-service/src/handlers/importFileParser.js
Expand Up @@ -9,9 +9,9 @@ const pipeline = promisify(_pipeline);

const {
IMPORT_S3_BUCKET,
IMPORT_S3_PREFIX,
IMPORT_S3_UPLOAD_PREFIX,
IMPORT_S3_REGION,
PARSED_S3_PREFIX,
IMPORT_S3_PARSED_PREFIX,
} = process.env;

export const handler = middy(importFileParser).use([middyRequestLogger()]);
Expand All @@ -21,7 +21,10 @@ export async function importFileParser(event, context, callback) {

const tasks = event.Records.map(async (record) => {
const srcKey = record.s3.object.key;
const destKey = srcKey.replace(IMPORT_S3_PREFIX, PARSED_S3_PREFIX);
const destKey = srcKey.replace(
IMPORT_S3_UPLOAD_PREFIX,
IMPORT_S3_PARSED_PREFIX
);

// parse CSV
const uploadedObject = s3.getObject({
Expand Down
8 changes: 6 additions & 2 deletions packages/import-service/src/handlers/importProductsFile.js
Expand Up @@ -5,7 +5,11 @@ import middyHttpCors from "@middy/http-cors";
import middyErrorHandler from "middy-error-handler";
import middyRequestLogger from "middy-request-logger";

const { IMPORT_S3_BUCKET, IMPORT_S3_PREFIX, IMPORT_S3_REGION } = process.env;
const {
IMPORT_S3_BUCKET,
IMPORT_S3_UPLOAD_PREFIX,
IMPORT_S3_REGION,
} = process.env;
const ALLOWED_CONTENT_TYPES = [
"text/csv",
"application/vnd.ms-excel",
Expand All @@ -29,7 +33,7 @@ export async function importProductsFile(event, context, callback) {
}

const s3 = new AWS.S3({ region: IMPORT_S3_REGION });
const uploadPath = [IMPORT_S3_PREFIX, fileName].join("");
const uploadPath = IMPORT_S3_UPLOAD_PREFIX + fileName;
const url = await s3.getSignedUrlPromise("putObject", {
Bucket: IMPORT_S3_BUCKET,
Key: uploadPath,
Expand Down