implement ipfs upload

This commit is contained in:
neil 2022-10-25 13:31:56 +08:00
parent dc4b97bbcb
commit b0623c52a6
3 changed files with 740 additions and 11 deletions

View file

@ -14,10 +14,13 @@
"@middy/http-json-body-parser": "^3.4.0",
"airtable": "^0.11.5",
"algoliasearch": "^4.14.2",
"aws-sdk": "^2.1234.0",
"aws-lambda": "^1.0.7",
"aws-sdk": "^2.1239.0",
"axios": "^1.1.3",
"compare-versions": "^5.0.1",
"lodash": "^4.17.21"
"ipfs-http-client": "^59.0.0",
"lodash": "^4.17.21",
"node-fetch": "^3.2.10"
},
"devDependencies": {
"@serverless/typescript": "^3.0.0",

View file

@ -1,4 +1,42 @@
const ipfsUpload = async () => {
console.log("uploaded!");
import AWS from 'aws-sdk';
import { S3CreateEvent } from 'aws-lambda';
import { create } from 'ipfs-http-client';
const s3 = new AWS.S3();
const ipfsUpload = async (event: S3CreateEvent) => {
const http = create({ url: 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');
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
console.log({cidHash})
console.log("process.env.BUCKET: ", process.env.BUCKET)
console.log("Path: ", record.s3.object.key + ".cid")
const s3Ack = await s3.putObject({
Bucket: process.env.AWS_DIST_BUCKET,
Key: fileName + ".cid",
Body: cidHash
}).promise()
console.log({s3Ack})
} catch (error) {
console.error(error);
}
}
}
export const main = ipfsUpload;

File diff suppressed because it is too large Load diff