mirror of
https://github.com/ivabus/www
synced 2024-11-22 19:05:05 +03:00
convert ipfs uploader to typscript
This commit is contained in:
parent
b0623c52a6
commit
14d6f79d75
5 changed files with 992 additions and 606 deletions
|
@ -9,6 +9,9 @@
|
|||
"engines": {
|
||||
"node": ">=14.15.0"
|
||||
},
|
||||
"resolutions": {
|
||||
"node-fetch": "2.6.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"@middy/core": "^3.4.0",
|
||||
"@middy/http-json-body-parser": "^3.4.0",
|
||||
|
@ -18,9 +21,9 @@
|
|||
"aws-sdk": "^2.1239.0",
|
||||
"axios": "^1.1.3",
|
||||
"compare-versions": "^5.0.1",
|
||||
"ipfs-http-client": "^59.0.0",
|
||||
"ipfs-http-client": "40.0.0",
|
||||
"lodash": "^4.17.21",
|
||||
"node-fetch": "^3.2.10"
|
||||
"node-fetch": "2.6.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@serverless/typescript": "^3.0.0",
|
||||
|
|
|
@ -1,23 +1,24 @@
|
|||
import AWS from 'aws-sdk';
|
||||
import { S3CreateEvent } from 'aws-lambda';
|
||||
import { create } from 'ipfs-http-client';
|
||||
import ipfs from 'ipfs-http-client';
|
||||
|
||||
const s3 = new AWS.S3();
|
||||
|
||||
const ipfsUpload = async (event: S3CreateEvent) => {
|
||||
const http = create({ url: process.env.IPFS_IP4_ADDRESS });
|
||||
const http = ipfs(process.env.IPFS_IP4_ADDRESS);
|
||||
|
||||
// S3 interaction
|
||||
for (const record of event.Records){
|
||||
try {
|
||||
const isPackage = record.s3.object.key.split('/').length > 0;
|
||||
const isCid = record.s3.object.key.includes('.cid');
|
||||
const { key } = record.s3.object;
|
||||
const isPackage = key.split('/').length > 1;
|
||||
const isCid = key.includes('.cid');
|
||||
if(!isPackage || isCid) continue
|
||||
|
||||
|
||||
// const objectURL = "https://" + process.env.BUCKET + ".s3.amazonaws.com/" + record.s3.object.key
|
||||
const fileName = decodeURIComponent(record.s3.object.key.replace(/\+/g, " "))
|
||||
const objectURL = "https://s3.amazonaws.com/" + process.env.AWS_DIST_BUCKET + "/" + fileName
|
||||
|
||||
|
||||
const cid = await http.add(objectURL)
|
||||
console.log({objectURL, cid})
|
||||
const cidHash = cid[0].hash
|
||||
|
|
|
@ -4,13 +4,30 @@ import { handlerPath } from '@libs/handler-resolver';
|
|||
export default {
|
||||
handler: `${handlerPath(__dirname)}/handler.main`,
|
||||
events: [
|
||||
// {
|
||||
// s3: {
|
||||
// bucket: '${ssm:AW5_S3_BUCKET}',
|
||||
// existing: true,
|
||||
// event: 's3:ObjectCreated:*'
|
||||
// }
|
||||
// }
|
||||
{
|
||||
s3: {
|
||||
bucket: '${ssm:AW5_S3_BUCKET}',
|
||||
existing: true,
|
||||
event: 's3:ObjectCreated:Post',
|
||||
rules: [
|
||||
{
|
||||
suffix: '.gz'
|
||||
},
|
||||
]
|
||||
},
|
||||
},
|
||||
{
|
||||
s3: {
|
||||
bucket: '${ssm:AW5_S3_BUCKET}',
|
||||
existing: true,
|
||||
event: 's3:ObjectCreated:Post',
|
||||
rules: [
|
||||
{
|
||||
suffix: '.xz'
|
||||
},
|
||||
]
|
||||
},
|
||||
},
|
||||
],
|
||||
environment: {
|
||||
AWS_DIST_BUCKET: '${ssm:AW5_S3_BUCKET}',
|
||||
|
|
25
lambdas/src/functions/ipfsUpload/mock.json
Normal file
25
lambdas/src/functions/ipfsUpload/mock.json
Normal file
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"Records": [
|
||||
{
|
||||
"s3": {
|
||||
"object": {
|
||||
"key": "zlib.net/v1.2.12.tar.gz"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"s3": {
|
||||
"object": {
|
||||
"key": "test/test.cid"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"s3": {
|
||||
"object": {
|
||||
"key": "packages.json"
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
1522
lambdas/yarn.lock
1522
lambdas/yarn.lock
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue