-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
74 lines (60 loc) · 2.05 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import dotenv from 'dotenv'
import IPFS from 'ipfs-core'
import toBuffer from 'it-to-buffer'
import { createS3Repo } from './create-s3-repo.js'
import S3 from 'aws-sdk/clients/s3.js'
import { S3Lock } from './s3-lock.js'
import { randomBytes } from 'crypto'
dotenv.config()
const BUCKET = process.env.S3BUCKET || 'changeme'
const ACCESSKEYID = process.env.ACCESS_KEY_ID || 'changeme'
const SECRET_ACCESS_KEY = process.env.SECRET_ACCESS_KEY || 'change'
async function main () {
// Configure S3 as normal
const s3 = new S3({
params: {
Bucket: BUCKET
},
accessKeyId: ACCESSKEYID,
secretAccessKey: SECRET_ACCESS_KEY
})
// Prevents concurrent access to the repo, you can also use the memory or fs locks
// bundled with ipfs-repo though they will not prevent processes running on two
// machines accessing the same repo in parallel
const repoLock = new S3Lock(s3)
// Create the repo
const s3Repo = createS3Repo('/', s3, repoLock)
// Create a new IPFS node with our S3 backed Repo
console.log('Start ipfs')
const node = await IPFS.create({
repo: s3Repo
})
// Test out the repo by sending and fetching some data
console.log('IPFS is ready')
try {
const version = await node.version()
console.log('Version:', version.version)
// Once we have the version, let's add a file to IPFS
const { path, cid } = await node.add({
path: 'data.txt',
content: Buffer.from(randomBytes(1024 * 25))
})
console.log('\nAdded file:', path, cid)
// Log out the added files metadata and cat the file from IPFS
const data = await toBuffer(node.cat(cid))
// Print out the files contents to console
console.log(`\nFetched file content containing ${data.byteLength} bytes`)
} catch (err) {
// Log out the error
console.log('File Processing Error:', err)
}
// After everything is done, shut the node down
// We don't need to worry about catching errors here
console.log('\n\nStopping the node')
await node.stop()
}
main()
.catch(err => {
console.error(err)
process.exit(1)
})