-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
342 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
PUBLIC_WALLETCONNECT_PROJECT_ID= | ||
PUBLIC_IPFS_GATEWAY= | ||
PUBLIC_IPFS_GATEWAY=https://taikoons.4everland.link/ipfs/ | ||
PUBLIC_LAUNCH_DATE=2024-05-26T00:00:00 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
const { S3 } = require("@aws-sdk/client-s3"); | ||
const { Upload } = require("@aws-sdk/lib-storage"); | ||
const fs = require("fs"); | ||
const fsPromises = fs.promises; | ||
const path = require("path"); | ||
|
||
async function uploadFile(s3, params) { | ||
try { | ||
const task = new Upload({ | ||
client: s3, | ||
queueSize: 3, // 3 MiB | ||
params, | ||
}); | ||
|
||
const res = await task.done(); | ||
return res.ETag.split('"').join(""); | ||
} catch (error) { | ||
if (error) { | ||
console.log("task", error.message); | ||
} | ||
} | ||
} | ||
|
||
// Helper function to form the metadata JSON object | ||
function populateNFTMetadata(name, description, CID) { | ||
return { | ||
name, | ||
description, | ||
image: CID, | ||
}; | ||
} | ||
|
||
async function main() { | ||
const s3Params = { | ||
accessKey: "KYEKUCIU01RUW4LL5M26", | ||
secretKey: "f+htmVYyj9IMN1XOoJfuVuYr08P6vb4ZW2xhxIQM", | ||
}; | ||
const { accessKey, secretKey } = s3Params; | ||
const s3 = new S3({ | ||
endpoint: "https://endpoint.4everland.co", | ||
credentials: { | ||
accessKeyId: accessKey, | ||
secretAccessKey: secretKey, | ||
}, | ||
region: "4EVERLAND", | ||
}); | ||
|
||
// Get the images to upload from the local filesystem (/images) | ||
console.log(`Importing images from the images/ directory...`); | ||
const imgDirPath = path.join(path.resolve(__dirname, "../../data"), "images"); | ||
const filesName = await fsPromises.readdir(imgDirPath, (err) => { | ||
if (err) { | ||
console.log("Import from directory failed: ", err); | ||
} | ||
}); | ||
|
||
// Uploading images to IPFS | ||
console.log(`Uploading image data to IPFS...`); | ||
const imageCIDs = []; | ||
const imagesSummary = []; | ||
let imageCount = 1; | ||
const imagesName = filesName.filter((fileName) => fileName.includes(".png")); | ||
for await (const imageName of imagesName) { | ||
const imageFilePath = path.join( | ||
path.resolve(__dirname, "../../data"), | ||
"images", | ||
imageName, | ||
); | ||
const params = { | ||
Bucket: "taikoons-testbucket", | ||
Key: imageName, | ||
ContentType: "image/png", | ||
Body: fs.readFileSync(imageFilePath), | ||
}; | ||
|
||
const imageCID = await uploadFile(s3, params); | ||
|
||
imageCIDs.push(imageCID); | ||
imagesSummary.push({ imageCID, imageCount }); | ||
console.log(`Image ${imageCount} added to IPFS with CID of ${imageCID}`); | ||
imageCount++; | ||
} | ||
console.log(` `); | ||
|
||
// Add the metadata to IPFS | ||
console.log(`Adding metadata to IPFS...`); | ||
let taikoonId = 0; | ||
for await (const imageCID of imageCIDs) { | ||
taikoonId++; | ||
|
||
// write into a file | ||
fs.writeFileSync( | ||
path.join( | ||
path.resolve(__dirname, "../../data"), | ||
"metadata", | ||
`${taikoonId}.json`, | ||
), | ||
JSON.stringify( | ||
populateNFTMetadata( | ||
`Taikoon ${taikoonId}`, | ||
"A Taikoon", | ||
imageCID.toString(), | ||
), | ||
), | ||
); | ||
|
||
console.log( | ||
path.join( | ||
path.resolve(__dirname, "../../data"), | ||
"metadata", | ||
`${taikoonId}.json`, | ||
), | ||
); | ||
/* | ||
metadataCIDs.push(metadataCID); | ||
for (let i = 0; i < imagesSummary.length; i++) { | ||
if (imagesSummary[i].imageCID == imageCID) { | ||
imagesSummary[i].metadataCID = metadataCID; | ||
} | ||
} */ | ||
// console.log(`Metadata with image CID ${imageCID} added to IPFS with CID of ${metadataCID}`); | ||
} | ||
console.log(` `); | ||
/* | ||
fs.writeFileSync( | ||
path.join( | ||
path.resolve(__dirname, "../../data"), | ||
"metadata", | ||
"summary.json", | ||
), | ||
JSON.stringify({ imagesSummary }), | ||
); | ||
*/ | ||
/* | ||
const putObjectOutput = await s3.putObject({ | ||
Bucket: "bucketname", | ||
Key: "key", | ||
Body: "data content", | ||
}); | ||
// multipart upload | ||
const params = { | ||
Bucket, | ||
Key: file.name, | ||
Body: file, | ||
ContentType: file.type, | ||
}; | ||
try { | ||
const task = new Upload({ | ||
client: s3, | ||
queueSize: 3, // 3 MiB | ||
params, | ||
}); | ||
task.on("httpUploadProgress", (e) => { | ||
const progress = ((e.loaded / e.total) * 100) | 0; | ||
console.log(progress, e); | ||
}); | ||
await task.done(); | ||
} catch (error) { | ||
if (error) { | ||
console.log("task", error.message); | ||
} | ||
} */ | ||
} | ||
|
||
main(); |
Oops, something went wrong.