liuliaixue

当白云悠然褪去我已等不及登上高山仰望西方的天空但最美的不是夕阳是夜色如水月色流离


  • 首页

  • 归档

  • 标签

how-to-resize-image-with-aws-lambda

发表于 2018-06-05

如何用AWS lambda改变图片尺寸

Resize Images on the Fly with Amazon S3, AWS Lambda, and Amazon API Gateway

创建并配置桶

  • 创建一个桶 ${your_bucket_name}
  • 配置 权限/存储桶策略

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    {
    "Version": "2012-10-17",
    "Id": "${your_policy_Id}",
    "Statement": [
    {
    "Sid": "${your_policy_statement_sid}",
    "Effect": "Allow",
    "Principal": "*",//${your_statement_principal},if you don't konw that,use '*' as default, delete this comments when you use it
    "Action": [
    "s3:DeleteObject",
    "s3:GetObject",
    "s3:PutObject"
    ],
    "Resource": "arn:aws:s3:::${your_bucket_name}/*"
    }
    ]
    }
  • 启用 属性/静态网站托管, 索引文档填入index.html,将会用到静态网址 ${your_s3_static_hostname}

创建lamdba函数

  • 如果你想用lambda,应该先开通这个应用
  • 创建lamdba function,选择空白函数,下一步
  • 配置触发器,选择 API Gateway,安全性,选择打开,下一步
  • 名称 resize,运行语言 Node.js 6.10.0,代码输入种类 上传.zip
  • 环境变量 BUCKET is ${your_bucket_name},URL is ${your_s3_static_hostname}
  • 创建一个普通角色,下面是创建信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    {
    "Version": "2012-10-17",
    "Statement": [
    {
    "Effect": "Allow",
    "Action": [
    "logs:CreateLogGroup",
    "logs:CreateLogStream",
    "logs:PutLogEvents"
    ],
    "Resource": "arn:aws:logs:*:*:*"
    },
    {
    "Effect": "Allow",
    "Action": "s3:PutObject",
    "Resource": "arn:aws:s3:::${your_bucket_name}/*"
    }
    ]
    }
    • 内存选择 1536,超时选择 10s,下一步,创建

    设置s3重定向

    • lambda.resize/触发器,复制这个触发器的主网址${your_trigger_hostname}
      //such as your_trigger_host = ‘xxxxxxxxx.execute-api.us-west-2.amazonaws.com’;
    • s3/${your_bucket_name}/属性/静态网站托管/重定向规则
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
        <RoutingRules>
      <RoutingRule>
      <Condition>
      <KeyPrefixEquals/>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
      </Condition>
      <Redirect>
      <Protocol>https</Protocol>
      <HostName>${your_trigger_hostname}</HostName>
      <ReplaceKeyPrefixWith>prod/${your_lambda_resize_name}?key=</ReplaceKeyPrefixWith>
      <HttpRedirectCode>307</HttpRedirectCode>
      </Redirect>
      </RoutingRule>
      </RoutingRules>

测试

上传一张图片 test.jpg

1
2
3
http://${your_s3_static_hostname}/300×300/test.jpg
http://${your_s3_static_hostname}/25×25/test.jpg
http://${your_s3_static_hostname}/500×500/test.jpg

完

Docker:memcached

发表于 2018-06-05

docker pull memcached:1.5

docker run –name d-memcached -p 11211:11211 -d memcached:1.5

Docker:mongo

发表于 2018-06-05

install mongo

1
2
sudo docker search mongo
sudo docker pull mongo:3.2

show images list

1
sudo docker images
1
2
3
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
mongo 3.2 c2a3c30cef05 9 days ago 301MB
hello-world latest f2a91732366c 2 weeks ago 1.85kB

run mongo

1
2
docker run  --name some-mongo   -p 27017:27017   -d mongo:3.2
sudo docker ps
1
2
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
ab77b469d968 mongo:3.2 "docker-entrypoint.s…" 25 seconds ago Up 24 seconds 0.0.0.0:27017->27017/tcp some-mongo

how to verify mongo running

open http://localhost:27017/, will show

It looks like you are trying to access MongoDB over HTTP on the native driver port.

using docker container bash

1
2
3
4
5
6
sudo docker exec -it <container_ID> sh
# mongo
# > db.user.insert({name:"alan"});
# > db.user.find();
# > exit
# exit

backup mongo

1
2
sudo docker cp <container_ID>:/data/db ./20170101
ls 20170101/

stop container

1
sudo docker stop <ocntainer_ID>

some useful cmd

1
2
sudo docker container ls -a // list all container, include stopped
sudo docker start <container_name> start a stopped container

docker cp

docker cp mycontainer:/opt/testnew/file.txt /opt/test/
docker cp /opt/test/file.txt mycontainer:/opt/testnew/

sudo docker cp /home/liuli/backup/dump/* cn-prod:/dump

sudo docker cp 2a1296fae066:/data/db /home/ubuntu/mongo-backup/04-23-2018-empty
sudo docker cp /home/ubuntu/mongo-backup/12-21-2017/* 2a1296fae066:/data/db

javascript 变量提升

发表于 2018-05-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
try {
function test_1() {
print('hello');
const print = (string) => { console.log(string); }
}
test_1();
} catch (error) {
console.log('test_1', error)
}

try {
function test_2() {
print('hello');
function print(string) { console.log(string); }
}
test_2();
} catch (error) {
console.log('test_2', error)
}

try {
function test_3() {
print('hello');
}
const print = (string) => { console.log(string); }
test_3();
} catch (error) {
console.log('test_3', error)
}

try {
function test_4() {
print('hello');
}
test_4();
const print = (string) => { console.log(string); }
} catch (error) {
console.log('test_4', error)
}


try {
function test_5() {
print('hello');
}
setTimeout(() => {
test_5()
}, 100);
const print = (string) => { console.log(string); }
} catch (error) {
console.log('test_5', error)
}

nodejs sleep

发表于 2018-05-01

const tag = ‘[sleep.js]’;

const sleepFn = async (ms) => {
await new Promise(resolve => {
setTimeout(() => { }, ms)
})
}

const sleep = async () => {
await new Promise(resolve => {
setTimeout(() => { resolve()}, 2000)
})
}

module.exports = {
sleepFn,
sleep
}

mongo 中级 游标

发表于 2018-05-01

mongodb 游标

在查询mongodb时, 有时需要 遍历 整个 collection或者 批处理大量的数据,用普通的find()会消耗大量内存且不便于操作

用法

1
db.collection.find(query, projection);
参数 类型 描述
query document 可选. 使用查询操作符指定查询条件
projection document 指定使用投影运算符返回的字段省略此参数返回匹配文档中的所有字段
1
2
3
4
5
6
7
8
9
async function readDataFromDB(db, col, query, projection) {
let resultArr = [];

const cursor = db.collection(col).find(query, projection);
for (let doc = await cursor.next(); doc != null; doc = await cursor.next()){
resultArr.push(doc);
}
return await Promise.all(resultArr);
}

nodejs remove folder

发表于 2018-05-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
const fs = require('fs');
const path = require('path')
const folder = '/tmp/ttcc';
const filename = '/tmp/ttx/task.sql';
if(0) {
const filenames = fs.readdirSync('/tmp/ttcc');
console.log(filenames)
for(let filename of filenames) {
const file = path.resolve(folder, filename);
console.log(file)
fs.unlinkSync(file)
}
fs.rmdirSync(folder)
}

if(1){
const folder = path.dirname(filename);
console.log(folder)
fs.unlinkSync(filename);
fs.rmdirSync(folder)
}

linux下常用命令

发表于 2018-05-01

linux 查看命令所在目录

1
2
whereis git
which git

linux 查看文件信息

推荐三种方法

1
2
3
ll /usr/bin/git
ls -l /usr/bin/git
ls -lh /usr/bin/git

更改文件(夹)权限

1
sudo chmod -R 777 filepath

安装vscode.deb

1
2
3
// ref https://code.visualstudio.com/docs/setup/linux
sudo dpkg -i <file>.deb
sudo apt-get install -f # Install dependencies

node (tar.gz)

$ wget https://nodejs.org/dist/v8.9.3/node-v8.9.3.tar.gz
$ tar zxvf node-v8.9.3.tar.gz

$ ./configure
$ sudo make && sudo make install

node(tar.xz)

1
2
3
4
5
$ wget https://nodejs.org/dist/v8.9.3/node-v8.9.3-linux-x64.tar.xz
$ xz -d node-v8.9.3-linux-x64.tar.xz
$ tar xvf node-v8.9.3-linux-x64.tar

$ vi ~/.bashrc

add below to you env

1
2
export NODE_HOME=/home/ubuntu/node-v8.9.3-linux-x64
export PATH=$NODE_HOME/bin:$PATH

linux 查看自己是否被 ping

1
2
3
4
# view ip
ifconfig -a
# listen ping via opening tcp listener
sudo tcpdump

ssh 登录到 vm

sudo apt-get install openssh-server
ssh @

how to install docker in ubuntu

https://docs.docker.com/engine/installation/linux/docker-ce/ubuntu/

tar.gz & tar.bz2

1
2
3
4
#.tar.gz            
tar -zxvf xx.tar.gz
#.tar.bz2
tar -jxvf xx.tar.bz2

mongo可视化工具

发表于 2018-05-01

download robo3t.tar.gz

link https://robomongo.org/download

tar

1
tar zxvf package.tar.gz

start robo3t error

1
2
3
4
5
6
7
This application failed to start because it could not find or load the Qt platform plugin "xcb"
in "".

Available platform plugins are: xcb.

Reinstalling the application may fix this problem.
Aborted (core dumped)

how to fix

1
2
3
1 mkdir ~/robo-backup
2 mv robo3t-1.1.1-linux-x86_64-c93c6b0/lib/libstdc++* ~/robo-backup/
3 robo3t-1.1.1-linux-x86_64-c93c6b0/bin/robo3t

ref:
https://www.cnblogs.com/hexin0614/p/7487191.html
https://askubuntu.com/questions/941383/cannot-run-robo-3t-qt-app
https://github.com/Studio3T/robomongo/issues/1384

stripe 接入支付宝

发表于 2018-05-01
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Note left of alipay : ...
frontend -> backend : request source
backend -> billing : request
billing -> stripe : request
stripe --> frontend : sourceObj
Note left of frontend : frontend user authentication
frontend -> alipay : open alipay QR code page
Note right of alipay : PAY STRIPE the money
alipay -> stripe : money
alipay -> frontend : redirect to redirect.[return_url]
frontend -> backend : request charge
backend -> billing : request
billing -> stripe : request
stripe --> backend : charge finished
Note right of stripe : money to MODLEO
Note left of backend : create company
backend -> frontend : finished
Note left of frontend : redirect to plan page
123…6
Alan

Alan

55 日志
92 标签
© 2019 Alan
由 Hexo 强力驱动
主题 - NexT.Muse