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": { "engines": {
"node": ">=14.15.0" "node": ">=14.15.0"
}, },
"resolutions": {
"node-fetch": "2.6.7"
},
"dependencies": { "dependencies": {
"@middy/core": "^3.4.0", "@middy/core": "^3.4.0",
"@middy/http-json-body-parser": "^3.4.0", "@middy/http-json-body-parser": "^3.4.0",
@ -18,9 +21,9 @@
"aws-sdk": "^2.1239.0", "aws-sdk": "^2.1239.0",
"axios": "^1.1.3", "axios": "^1.1.3",
"compare-versions": "^5.0.1", "compare-versions": "^5.0.1",
"ipfs-http-client": "^59.0.0", "ipfs-http-client": "40.0.0",
"lodash": "^4.17.21", "lodash": "^4.17.21",
"node-fetch": "^3.2.10" "node-fetch": "2.6.7"
}, },
"devDependencies": { "devDependencies": {
"@serverless/typescript": "^3.0.0", "@serverless/typescript": "^3.0.0",

View file

@ -1,23 +1,24 @@
import AWS from 'aws-sdk'; import AWS from 'aws-sdk';
import { S3CreateEvent } from 'aws-lambda'; import { S3CreateEvent } from 'aws-lambda';
import { create } from 'ipfs-http-client'; import ipfs from 'ipfs-http-client';
const s3 = new AWS.S3(); const s3 = new AWS.S3();
const ipfsUpload = async (event: S3CreateEvent) => { const ipfsUpload = async (event: S3CreateEvent) => {
const http = create({ url: process.env.IPFS_IP4_ADDRESS }); const http = ipfs(process.env.IPFS_IP4_ADDRESS);
// S3 interaction // S3 interaction
for (const record of event.Records){ for (const record of event.Records){
try { try {
const isPackage = record.s3.object.key.split('/').length > 0; const { key } = record.s3.object;
const isCid = record.s3.object.key.includes('.cid'); const isPackage = key.split('/').length > 1;
const isCid = key.includes('.cid');
if(!isPackage || isCid) continue if(!isPackage || isCid) continue
// const objectURL = "https://" + process.env.BUCKET + ".s3.amazonaws.com/" + record.s3.object.key // const objectURL = "https://" + process.env.BUCKET + ".s3.amazonaws.com/" + record.s3.object.key
const fileName = decodeURIComponent(record.s3.object.key.replace(/\+/g, " ")) const fileName = decodeURIComponent(record.s3.object.key.replace(/\+/g, " "))
const objectURL = "https://s3.amazonaws.com/" + process.env.AWS_DIST_BUCKET + "/" + fileName const objectURL = "https://s3.amazonaws.com/" + process.env.AWS_DIST_BUCKET + "/" + fileName
const cid = await http.add(objectURL) const cid = await http.add(objectURL)
console.log({objectURL, cid}) console.log({objectURL, cid})
const cidHash = cid[0].hash const cidHash = cid[0].hash

View file

@ -4,13 +4,30 @@ import { handlerPath } from '@libs/handler-resolver';
export default { export default {
handler: `${handlerPath(__dirname)}/handler.main`, handler: `${handlerPath(__dirname)}/handler.main`,
events: [ events: [
// { {
// s3: { s3: {
// bucket: '${ssm:AW5_S3_BUCKET}', bucket: '${ssm:AW5_S3_BUCKET}',
// existing: true, existing: true,
// event: 's3:ObjectCreated:*' event: 's3:ObjectCreated:Post',
// } rules: [
// } {
suffix: '.gz'
},
]
},
},
{
s3: {
bucket: '${ssm:AW5_S3_BUCKET}',
existing: true,
event: 's3:ObjectCreated:Post',
rules: [
{
suffix: '.xz'
},
]
},
},
], ],
environment: { environment: {
AWS_DIST_BUCKET: '${ssm:AW5_S3_BUCKET}', 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