-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
57 lines (44 loc) · 1.53 KB
/
demo.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
import { Bee } from '@ethersphere/bee-js'
import { promises as fs } from 'fs'
import path from 'path'
const BEE_URL = 'http://127.0.0.1:1633'
const bee = new Bee(BEE_URL)
// Function to create or retrieve an existing postage batch
async function getOrCreatePostageBatch() {
let batchId
const batches = await bee.getAllPostageBatch()
const usable = batches.find(x => x.usable)
if (usable) {
batchId = usable.batchID
} else {
batchId = await bee.createPostageBatch('500000000', 17)
}
return batchId
}
// Function to upload file data with redundancy and encryption
async function uploadFileWithRedundancyAndEncryption(filePath) {
const batchId = await getOrCreatePostageBatch()
const fileData = await fs.readFile(filePath, 'utf-8')
const options = {
encrypt: true,
redundancyLevel: 1, // Set redundancy level
}
const uploadResult = await bee.uploadData(batchId, fileData, options)
return uploadResult.reference
}
// Function to download data
async function downloadData(reference) {
const downloadResult = await bee.downloadData(reference)
const data = await downloadResult.text()
return data
}
async function main() {
const filePath = path.resolve('test.txt')
console.log('Uploading file...')
const reference = await uploadFileWithRedundancyAndEncryption(filePath)
console.log(`File uploaded with reference: ${reference}`)
console.log('Downloading file...')
const downloadedData = await downloadData(reference)
console.log(`Downloaded file data: ${downloadedData}`)
}
main().catch(console.error)