In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the method of ipfs-api accessing IPFS", so the editor summarizes the following contents, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "what is the method of ipfs-api accessing IPFS?"
II. Installation and use of ipfs-api
The interstellar file system Ipfs node provides and REST API interface, which allows us to operate the node in the program code to upload files and other operations. In most cases, however, we do not need to directly manipulate the REST development interface, but instead use the packaged and friendlier ipfs-api, a nodejs package.
2.1 install nodejs
Download the nodejs installation package from the official website: 32-bit, 64-bit. After download, double-click to install.
Open a console window and test:
C:\ Users\ hubwiz > node-vV8.11.12.2 install ipfs-api
The installation of Ipfs-api requires the git command line, so let's install git first. Download the git installation package from the official website: 32-bit, 64-bit. After downloading, double-click to install.
Execute the git command test:
D:\ test-ipfs-api > git versiongit version 2.16.2.windows.1
Ipfs-api needs to compile the native node module, so you need to install VisualStudio 2015 and python27.
Download VisualStudio 2015 Community Edition on the official website and double-click to install it. Download the Python27 installation package on the official website, double-click to install and set the PATH environment variable to make python available.
Reopen a console to make the environment variables take effect. Now install ipfs-api:
D:\ test-ipfs-api > npm install ipfs-api+ ipfs-api@20.0.1added 1 package, updated 1 package and moved 1 package in 22.138s2.3 Test Code-nodejs
Create a test script test.js in the D:\ test-ipfs-api directory:
Const ipfsAPI = require ('ipfs-api') const ipfs = ipfsAPI (' localhost', '5001mm, {protocol:' http'}) const buffer = Buffer.from ('this is a demo') ipfs.add (buffer). Then (rsp = > console.log (rsp [0] .hash)) .catch (err = > console.error (err))
Execute this script:
D:\ test-ipfs-api > node test.jsQmfQS4vm9YZTAyGZEkDqm81xripwsK3NgqfNkbCdoeEw5i
That is, after we add the content this is a demo to the local repository, we get the hash value QmfQS4vm9YZTAyGZEkDqm81xripwsK3NgqfNkbCdoeEw5i. You can now use the cat subcommand to view the contents of this hash:
D:\ test-ipfs-api > ipfs cat QmfQS4vm9YZTAyGZEkDqm81xripwsK3NgqfNkbCdoeEw5i
The console will output what we uploaded earlier:
This is a demo
After ipfs enters the listening state, a http gateway is provided so that we can use a browser to access the content on the ipfs. By default, the gateway listens on port 8080 of the native (127.0.0.1), so use your browser to access this URL:
Http://127.0.0.1:8080/ipfs/QmfQS4vm9YZTAyGZEkDqm81xripwsK3NgqfNkbCdoeEw5i
You can also see what we uploaded earlier. Note: you need to start the listener (ipfs daemon) first and your browser and ipfs node are on the same computer. ?
Third, access ipfs in the browser
Ipfs-api is also supported in browser. The easiest way is to use a browser-specific wrapper library, which can be referenced in html:
This specially encapsulated library creates a global object ipfsAPI, which we can use directly in browser scripts, such as:
Var ipfs = window.IpfsApi ('localhost',' 5001')
This method is relatively simple, so it will not be described below. Next we will use a more engineering approach, using webpack to use ipfs-api 's nodejs package directly in the front-end script.
3.1 HTML page
Create an index.html under the D:\ test-ipfs-api directory:
THIS IS ANOTHER DEMO Upload
Our goal is to upload the contents of the text box to ipfs when the button is clicked
3.2 Front-end script
Write a script app.js in the D:\ test-ipfs-api directory:
Import ipfsAPI from 'ipfs-api'const ipfs = ipfsAPI (' localhost', '5001mm, {protocol:' http'}) window.addEventListener ('load', function () {let btn = document.querySelector (' # upload') let txt = document.querySelector ('# content') btn.addEventListener ('click', () = > {let buffer = Buffer.from (txt.value,' utf-8')) Ipfs.add (buffer) .then (rsp = > console.log (rsp [0] .hash)) .catch (err = > console.error (err))}) 3.3 webpack configuration
Write the configuration file webpack.config.js in the D:\ test-ipfs-api directory:
Const webpack = require ('webpack'); const path = require (' path'); const CopyWebpackPlugin = require ('copy-webpack-plugin') Module.exports = {entry:'. / app.js', output: {path: path.resolve (_ _ dirname), filename: 'bundle.js'}, module: {loaders: [{test: /\ .js $/, exclude: / (node_modules | bower_components) /, loader:' babel-loader', query: {presets: ['es2015'] Plugins: ['transform-runtime']} 3.4 frontend script packaging
Perform webpack packaging:
D:\ test-ipfs-api > webpack3.5 configure CORS policy for ipfs
Since the ipfs node needs to be accessed from the web page, which introduces cross-domain security issues, we need to configure the ipfs node to allow cross-domain requests:
D:\ > ipfs config-- json API.HTTPHeaders.Access-Control-Allow-Origin'["*"] '3.6 configure the API listening address of ipfs
Since the ipfs node listens for API requests on port 5001 of the native (127.0.0.1) by default, if your browser and ipfs node are not on the same machine, you need to let the ipfs node listen for public addresses:
D:\ > ipfs config-- json Addresses.API'"/ ip4/0.0.0.0/tcp/5001"'
Of course, if your browser and ipfs node are on the same machine, you don't need to configure this.
3.7 configure the listening address of the gateway of ipfs
Since the http gateway of the ipfs node listens for http requests on port 8080 of the native (127.0.0.1) by default, if your browser and ipfs node are not on the same machine, you need to let the ipfs gateway listen on the public address:
D:\ > ipfs config-- json Addresses.Gateway'"/ ip4/0.0.0.0/tcp/8080" '3.8 test web page
First, start ipfs snooping:
D:\ > ipfs daemon
Then start the web server in the test directory, where you can use the simple server built into python, of course, you can use any familiar web server:
D:\ test-ipfs-api > python-m SimpleHTTPServerServing HTTP on 0.0.0.0 port 8000...
Now open your browser and visit http://127.0.0.1:8000/. If all goes well, you can see a text box and a button. Click the button to upload the contents of the text box to the ipfs node.
The above is the content of this article on "what is the method of ipfs-api accessing IPFS". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.