convert ipfs uploader to typscript

This commit is contained in:
neil 2022-10-27 14:11:45 +08:00
parent b0623c52a6
commit 14d6f79d75
5 changed files with 992 additions and 606 deletions

View file

@ -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",

View file

@ -1,17 +1,18 @@
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

View file

@ -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}',

View 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"
}
}
}
]
}

File diff suppressed because it is too large Load diff