ipfs-file-manager/README.md

52 lines
1.5 KiB
Markdown
Raw Normal View History

2023-06-19 15:07:25 +00:00
# IPFS File Manager
This repository contains JavaScript code to interact with IPFS (InterPlanetary File System). It provides functionalities to add and retrieve files from IPFS.
2023-06-19 15:07:25 +00:00
## Adding a File to IPFS
2023-06-19 15:07:25 +00:00
To add a file to IPFS, use the following code:
2023-06-19 15:07:25 +00:00
```javascript
import { create } from 'ipfs';
import { writeFile } from 'fs/promises';
2023-06-19 15:07:25 +00:00
class IPFSFileManager {
// ...
}
const ipfsFileManager = new IPFSFileManager();
2023-06-19 15:07:25 +00:00
const filePath = './file.txt';
ipfsFileManager.addFileToIPFS(filePath);
2023-06-19 15:07:25 +00:00
```
Make sure to replace `filePath` with the path of the file you want to add to IPFS.
2023-06-19 15:07:25 +00:00
## Retrieving a File from IPFS
2023-06-19 15:07:25 +00:00
To retrieve a file from IPFS, use the following code:
2023-06-19 15:07:25 +00:00
```javascript
import { create } from 'ipfs';
import { writeFile } from 'fs/promises';
import { fileTypeFromBuffer } from 'file-type';
2023-06-19 15:07:25 +00:00
class IPFSFileManager {
// ...
}
const ipfsFileManager = new IPFSFileManager();
const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve
const filepath = './retrievedFile'; // Specify the file path without the extension
ipfsFileManager.retrieveFileFromIPFS(cid, filepath);
2023-06-19 15:07:25 +00:00
```
Make sure to replace `cid` with the CID of the file you want to retrieve from IPFS.
2023-06-19 15:07:25 +00:00
## Notes
2023-06-19 15:07:25 +00:00
- Make sure you have Node.js installed in your development environment.
- Install the necessary dependencies before running the code. You can use npm or yarn to install the dependencies specified in the `package.json` file.
2023-06-19 15:07:25 +00:00
Feel free to explore and modify the code according to your needs.