[英]Is it possible to upload a string/js object without having it stored in a file? - IPFS
我一直在玩js-ipfs API,想問問是否有人知道Wheather js-ipfs僅支持文件/文件夾上傳? 有沒有一種上傳方式,例如javascript對象:
{
heading:"SomeHeading",
content:"somecontent"
}
或類似的字符串
"{heading:\"SomeHeading\", content:\"somecontent\"}"
到目前為止,我已經嘗試過:
const ipfs = window.IpfsApi('localhost', 5001, {protocol: 'https'});
const buffer = ipfs.Buffer;
async function uploadToIpfs() {
let someObject = {
heading:"SomeHeading",
content:"someContent"
};
let objectString = JSON.stringify(someObject);
let bufferedString = await buffer.from(objectString);
await ipfs.add(bufferedString, (err, resp) => {
console.log(err);
console.log(resp);
});
}
任何解決此問題的幫助,或者只是一個完整的答案,都可以僅上傳js對象或字符串,將不勝感激!
我嘗試了您的代碼,由於您在本地主機上,因此您所要做的就是將protocol
更改為http
。
{protocol: 'http'}
如果我正確理解了您的問題,則希望上傳任意對象的內容; 因此,您需要一個抽象blob存儲。 這可以使用ipfs-blob-store來完成。
確保閱讀文檔但有所了解:
var ipfsBlobStore = require('ipfs-blob-store')
var options = {
port: 5001, // default value
host: '127.0.0.1', // default value
baseDir: '/', // default value
flush: true // default value
}
var store = ipfsBlobStore(options)
var ws = store.createWriteStream({
key: 'some/path/file.txt'
})
ws.write("{heading:\"SomeHeading\", content:\"somecontent\"}")
ws.end(function() {
var rs = store.createReadStream({
key: 'some/path/file.txt'
})
rs.pipe(process.stdout)
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.