Skip to content

Instantly share code, notes, and snippets.

@SalvoCozzubo
SalvoCozzubo / move-file.js
Last active September 14, 2022 12:42
copy file between bucket
const { CopyObjectCommand, S3Client } = require('@aws-sdk/s3-client');
const client = new S3Client({});
const moveFile = async (sourceKey, destBucket, destKey) => {
const params = {
Bucket: destBucket,
Key: destKey,
CopySource: sourceKey,
};
@SalvoCozzubo
SalvoCozzubo / aws-sqs-direction-integration.ts
Last active July 23, 2022 15:46
AWS CDK api gateway direct integration with AWS SQS
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as IAM from 'aws-cdk-lib/aws-iam';
import * as sqs from 'aws-cdk-lib/aws-sqs';
import * as ApiGW from 'aws-cdk-lib/aws-apigateway';
export class AwsSqsDirectIntegrationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
@SalvoCozzubo
SalvoCozzubo / rekognition.js
Last active July 10, 2022 12:14
Detect Labels with AWS Rekognition
const { RekognitionClient, DetectLabelsCommand } = require('@aws-sdk/client-rekognition');
const client = new RekognitionClient({});
const getLabels = async (picture) => {
const params = {
Image: {
Bytes: picture,
},
MaxLabels: 20,
@SalvoCozzubo
SalvoCozzubo / send-sms.js
Created April 18, 2022 19:46
Send SMS with AWS SNS
import { SNSClient, PublishCommand } from '@aws-sdk/client-sns';
const client = new SNSClient({});
const handler = async (event) => {
if (!event.body) {
return { message: 'Body not found' }
}
const { numberPhone, text } = JSON.parse(event.body);
const params = {
@SalvoCozzubo
SalvoCozzubo / upload.js
Created March 6, 2022 11:41
Upload a file in S3 in Nodejs
const uploadFile = async () => {
const params = {
Bucket: process.env.BUCKET,
Key: 'file-upload.txt',
Body: 'hello world',
};
const upload = new Upload({
client,
params,
@SalvoCozzubo
SalvoCozzubo / upload-putObject.js
Created March 6, 2022 11:33
Upload a file in S3 in nodejs
const putFile = async () => {
const params = {
Bucket: process.env.BUCKET,
Key: 'file.txt',
Body: 'hello world',
};
return client.send(new PutObjectCommand(params));
};
@SalvoCozzubo
SalvoCozzubo / consumer.js
Created February 16, 2022 22:48
Partial Batch Failure SQS
module.exports.index = async (event) => {
const failedIds = event.Records.reduce((acc, item, index) => {
// half of records fail
if (index % 2 === 0) {
acc.push({ itemIdentifier: item.messageId });
}
return acc;
}, []);
@SalvoCozzubo
SalvoCozzubo / index.js
Created February 3, 2022 19:07
Handler
const { SSMClient, GetParameterCommand } = require('@aws-sdk/client-ssm');
const AWSXRay = require('aws-xray-sdk');
// code outside the handler
const client = AWSXRay.captureAWSv3Client(new SSMClient({}));
let value;
module.exports.handler = async () => {
// check if values is cached
@SalvoCozzubo
SalvoCozzubo / getObject.js
Created October 14, 2021 13:04
GetObject Example
const { S3Client, GetObjectCommand } = require('@aws-sdk/client-s3');
const client = new S3Client({});
const streamToString = (stream) => new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});
@SalvoCozzubo
SalvoCozzubo / streamToString.js
Last active October 14, 2021 11:18
S3 Stream to String
const streamToString = (stream) => new Promise((resolve, reject) => {
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('error', reject);
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')));
});