Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
marcieltorres / get_address_by_cep.py
Created Aug 25, 2018
ViaCEP: AWS Lambda Function para buscar endereço através do CEP
View get_address_by_cep.py
import json
import urllib
import re
def lambda_handler(event, context):
if 'cep' in event and _regex(event['cep']):
return json.loads(urllib.request.urlopen(_get_url_api(event['cep'])).read())
return json.loads("{\"erro\": true, \"mensagem\": \"Formato incorreto\"}")
@marcieltorres
marcieltorres / s3-aws-lambda-function-trigger-object.py
Created Aug 25, 2018
AWS Lambda function to trigger objects
View s3-aws-lambda-function-trigger-object.py
import boto3
import re
s3 = boto3.client('s3')
def lambda_handler(event, context):
if 'Records' in event:
bucket_object = event['Records'][0]['s3']['bucket']['name']
@marcieltorres
marcieltorres / aws-lambda-s3-validating-file.js
Created Aug 25, 2018
NodeJS: Validating s3 file with the AWS Lambda Function
View aws-lambda-s3-validating-file.js
//Using SDK
var aws = require('aws-sdk');
var s3 = new aws.S3();
//Define quais extensões serão aceitas no bucket
var _extensionsAllowed = ["txt", "csv"];
//Define tamanho máximo e mínimo que serão aceitos (em bytes)
var _minFileSize = 20; var _maxFileSize = (1024*1024*2);
@marcieltorres
marcieltorres / aws-lambda-s3-get-content-object.js
Created Aug 25, 2018
NodeJS: Getting the S3 object content with the AWS Lambda Function
View aws-lambda-s3-get-content-object.js
var aws = require('aws-sdk');
var s3 = new aws.S3();
exports.handler = (event, context, callback) => {
//Verifica se o objeto existe (chamada através de evento no S3)
if ((event) && (event.Records) && (event.Records[0]) && (event.Records[0].s3) && (event.Records[0].s3.bucket) && (event.Records[0].s3.object)){
//Recupera informações de bucket e do objeto
var _bucket = event.Records[0].s3.bucket.name;
@marcieltorres
marcieltorres / aws-lambda-function-send-mail.js
Created Aug 25, 2018
NodeJS: Sending mail with AWS Lambda Function
View aws-lambda-function-send-mail.js
var aws = require('aws-sdk');
exports.handler = (event, context, callback) => {
var _returnMessage = "";
var _destinationAddress, _sourceAddress = 'source@sourcedomain.com';
var _defaultSubject = '[Subject]';
var _defaultMessageData = '[Empty Message]';
//params SES
@marcieltorres
marcieltorres / aws-lambda-function-receitaws.py
Created Aug 25, 2018
Get data from ReceitaWS with the AWS Lambda Function
View aws-lambda-function-receitaws.py
import json
import urllib
import re
def lambda_handler(event, context):
if 'cnpj' in event and _regex(event['cnpj']):
return json.loads(urllib.request.urlopen(_getUrlApi(event['cnpj'])).read())
return json.loads("{\"status\": \"error\", \"message\": \"Formato incorreto\"}")
@marcieltorres
marcieltorres / get-mx-records.js
Created Aug 25, 2018
nodeJS-get-mx-records-from-domain
View get-mx-records.js
var dns = require('dns');
function getMxRecords(domain){
return new Promise(function(resolve, reject) {
dns.resolveMx(domain, function (err, addresses) {
if (err) {
//console.log(err, err.stack)
resolve(null);
}
@marcieltorres
marcieltorres / git-revert.txt
Last active May 27, 2018
git revert to commit
View git-revert.txt
git reset <commit id>
git reset --soft HEAD@{1}
git commit -m "Revert to <commit id>"
git reset --hard
@marcieltorres
marcieltorres / mapping.json
Last active Jan 19, 2018
API Gateway (AWS): Mapping Body Template (/url/{param})
View mapping.json
{
"body" : $input.json('$'),
"headers": {
#foreach($header in $input.params().header.keySet())
"$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
#end
},
"method": "$context.httpMethod",
"params": {
#foreach($param in $input.params().path.keySet())