Update README with installation instructions

This commit is contained in:
Luiz F Picolo 2023-06-20 18:36:02 -04:00
parent 44b8f4a924
commit a2bc9748a0

View File

@ -2,51 +2,55 @@
This repository contains JavaScript code to interact with IPFS (InterPlanetary File System). It provides functionalities to add and retrieve files from IPFS. This repository contains JavaScript code to interact with IPFS (InterPlanetary File System). It provides functionalities to add and retrieve files from IPFS.
## Adding a File to IPFS ## Installation
Before running the code, make sure you have Node.js installed in your development environment. You can download it from the official Node.js website: [https://nodejs.org](https://nodejs.org)
To install the necessary dependencies, navigate to the project directory and run the following command:
```bash
npm install
```
This will install the required packages specified in the `package.json` file.
## Usage
### Adding a File to IPFS
To add a file to IPFS, use the following code: To add a file to IPFS, use the following code:
```javascript ```javascript
import { create } from 'ipfs'; import IPFSFileUploader from './IPFSFileUploader.js';
import { writeFile } from 'fs/promises';
class IPFSFileManager { const ipfsUploader = new IPFSFileUploader();
// ...
}
const ipfsFileManager = new IPFSFileManager(); const filePath = './file.txt'; // Replace with the path of the file you want to add to IPFS
ipfsUploader.uploadFileToIPFS(filePath);
const filePath = './file.txt';
ipfsFileManager.addFileToIPFS(filePath);
``` ```
Make sure to replace `filePath` with the path of the file you want to add to IPFS. Make sure to replace `filePath` with the path of the file you want to add to IPFS.
## Retrieving a File from IPFS ### Retrieving a File from IPFS
To retrieve a file from IPFS, use the following code: To retrieve a file from IPFS, use the following code:
```javascript ```javascript
import { create } from 'ipfs'; import IPFSFileRetriever from './IPFSFileRetriever.js';
import { writeFile } from 'fs/promises';
import { fileTypeFromBuffer } from 'file-type';
class IPFSFileManager { const ipfsRetriever = new IPFSFileRetriever();
// ...
}
const ipfsFileManager = new IPFSFileManager();
const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve const cid = 'QmQgGxzWnzjCfouxRiozBiEG3wcsuJGWjtHjv3wurVbJ9s'; // Replace with the CID of the file you want to retrieve
const filepath = './retrievedFile'; // Specify the file path without the extension const filepath = './retrievedFile'; // Specify the file path without the extension
ipfsFileManager.retrieveFileFromIPFS(cid, filepath); ipfsRetriever.retrieveFileFromIPFS(cid, filepath);
``` ```
Make sure to replace `cid` with the CID of the file you want to retrieve from IPFS. Make sure to replace `cid` with the CID of the file you want to retrieve from IPFS.
## Notes ## Notes
- Make sure you have Node.js installed in your development environment. - The code depends on the IPFS package for Node.js. It will be installed automatically when you run `npm install`.
- Install the necessary dependencies before running the code. You can use npm or yarn to install the dependencies specified in the `package.json` file. - The code uses the `fs/promises` module for file operations, which is available in Node.js version 14.0.0 and above.
- Additional dependencies such as `file-type` may be required for file type determination. They will also be installed automatically.
Feel free to explore and modify the code according to your needs. Feel free to explore and modify the code according to your needs.