简体   繁体   English

是否可以在以太坊区块链上存储图像?

[英]Is it possible to store images on the Ethereum blockchain?

I'm ramping up on learning Solidity, and have some ideas.我正在加紧学习 Solidity,并有一些想法。 At the moment I am curious if files/images can be put on the blockchain.目前我很好奇文件/图像是否可以放在区块链上。 I'm thinking an alternative would be some hybrid approach where some stuff is on the blockchain, and some stuff is in a more traditional file storage and uses address references to grab it.我在想另一种方法是一些混合方法,其中一些东西在区块链上,一些东西在更传统的文件存储中,并使用地址引用来抓取它。 One issue I foresee is gas price of file uploads.我预见的一个问题是文件上传的汽油价格。

Is it possible to store images on the Ethereum blockchain? 是否可以在以太坊区块链上存储图像?

It's absolutely possible! 绝对可能!

Should you do it? 你应该这样做吗? Almost certainly not! 几乎肯定不是!

One issue I foresee is gas price of file uploads. 我预见的一个问题是文件上传的汽油价格。

  • The cost of data storage is 640k gas per kilobyte of data. 数据存储成本为每千字节数据640k气体。
  • The current gas price is approximately 15 Gwei (or 0.000000015 ETH). 目前的天然气价格约为15 Gwei(或0.000000015 ETH)。
  • At today's price, 1 ETH is approximately $200. 按今天的价格计算,1 ETH约为200美元。

That works out at just under $2 per kilobyte. 每千字节的成本不到2美元。

It's not for me to tell you if this is too expensive for your application, but you should also consider that the price of both Gas and Ether vary dramatically over time, and you should expect to experience periods when this number will be significantly higher. 我不能告诉你这是否对你的应用来说太昂贵了,但是你也应该考虑到Gas和Ether的价格随着时间的推移而变化很大,你应该期望这个数字会明显更高。

Note: I tried to store that +10,000 long base64 string of a 100kb image, but it did't accept.注意:我尝试存储 100kb 图像的 +10,000 长 base64 字符串,但它不接受。 but when i tried 1kb image, it worked.但是当我尝试 1kb 图像时,它起作用了。

yes.是的。 This is the solidity code to do it:-这是执行此操作的可靠代码:-

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract ImgStorage {
    uint i=0;
    mapping(uint => string[]) public base64_images;
    function push(string memory  base64_img) public {
        base64_images[i].push(base64_img);
        i++;        
    }
    function returnImage(uint n) public view returns(string[] memory){
        return base64_images[n];
    }
}

working code image:工作代码图像: 在此处输入图像描述

You can convert image to base64 and vise versa online.您可以在线将图像转换为 base64,反之亦然。 Here is NodeJS code to convert image to base64 string:这是将图像转换为 base64 字符串的 NodeJS 代码:

const imageToBase64 = require('image-to-base64');
const fs=require('fs')
imageToBase64("img/1kb.png") 
    .then(data => {fs.writeFile('1kb_png.md',data, (err)=>{console.log(err)})})
    .catch(err =>console.log(err))

Technically, yes, you could store very small images. 从技术上讲,是的,你可以存储非常小的图像。 But you shouldn't. 但你不应该。

Preferred Alternative 首选替代方案

Store the image in a distributed file store (for example, Swarm or IPFS), and store a hash of the image on-chain, if it's really important for the image to provably untampered. 将图像存储在分布式文件存储(例如,Swarm或IPFS)中,并将图像的哈希值存储在链上,如果图像对于可证明未被篡改非常重要。 If that's not important, then maybe don't put anything on chain. 如果这不重要,那么也许不要把任何东西放在链上。

What technical limit is there? 有什么技术限制?

Primarily, the block's gas limit. 主要是块的气体限制。 Currently, Ethereum mainnet has an 8Mgas block limit. 目前,以太坊主网拥有8Mgas的限制。 Every new 32bytes of storage uses 20k gas. 每个新的32字节存储使用20k气体。 So you can't store data that sums to more than 12.8kb, because it doesn't fit in the block. 因此,您无法存储总和超过12.8kb的数据,因为它不适合块。

Why shouldn't I use it for small files? 为什么我不应该将它用于小文件?

The blockchain wasn't designed for that usage (which is why other projects like Swarm and IPFS exist). 区块链不是为这种用法而设计的(这就是存在Swarm和IPFS等其他项目的原因)。 It bloats and slows everything down, without providing you any benefit over other file storage systems. 它会使所有内容变得臃肿并减速,而不会为您提供超过其他文件存储系统的任何好处。 By analogy, you typically don't store files in a SQL database, either. 通过类比,您通常也不会将文件存储在SQL数据库中。

I totally agree with @Peter Hall that storing the image on ethereum is too costly. 我完全赞同@Peter Hall将图像存储在以太坊上太昂贵了。


So, what you can do instead of this? 那么,你可以做什么而不是这个呢?

  • You can store the image on IPFS. 您可以将图像存储在IPFS上。 IPFS gives you a fixed length of a hash. IPFS为您提供固定长度的哈希值。 Now, you can store this hash on Ethereum and it cost less than another way. 现在,您可以将此哈希存储在以太坊上,并且它的成本低于另一种方式。

Storing images on-chain is an emphatic NO ! 在链上存储图像是一个有力的NO

Storing images in a database is also not a good practice, I'm assuming you just mean file storage solutions like S3 / firebase. 将图像存储在数据库中也不是一个好习惯,我假设你只是指S3 / firebase等文件存储解决方案。 Storing images on a central server is okay but it depends what you want to achieve, there are decentralized storage solutions like IPFS and Swarm that you could look into. 将图像存储在中央服务器上是可以的,但这取决于您想要实现的目标,您可以查看IPFS和Swarm等分散存储解决方案。

Ethereum is too heavy as well as expensive to store large blobs like images, video, and so on.以太坊太重而且太昂贵,无法存储图像、视频等大块。 Hence, some external storage is necessary to store bigger objects.因此,需要一些外部存储来存储更大的对象。 This is where the Interplanetary File System (IPFS) comes into the picture.这就是星际文件系统(IPFS)出现的地方。 The Ethereum Dapp can hold a small amount of data, whereas for saving anything more or bigger such as images, words, PDF files, and so on, we use IPFS.以太坊 Dapp 可以保存少量数据,而为了保存更多或更大的数据,例如图像、文字、PDF 文件等,我们使用 IPFS。

IPFS is an open-source protocol and network designed to create a peer-to-peer method of storing and sharing data. IPFS 是一种开源协议和网络,旨在创建一种存储和共享数据的点对点方法。 It is similar to Bit Torrent.它类似于 Bit Torrent。

If you want to upload a PDF, Word, or image file to IPFS.如果您想将 PDF、Word 或图像文件上传到 IPFS。

1- You put the PDF, Word, or image file in your working directory. 1- 您将 PDF、Word 或图像文件放在您的工作目录中。

2- You inform IPFS to add this file, which generates a hash of the file. 2- 您通知 IPFS 添加此文件,这会生成文件的哈希值。 Note an IPFS hash always starts with “Qm....”请注意,IPFS 哈希始终以“Qm ....”开头

3- Your file is available on the IPFS network. 3- 您的文件在 IPFS 网络上可用。

Now you uploaded the file and want to share the file with Bob.现在您上传了文件并希望与 Bob 共享该文件。 you send the hash of the file to Bob, Bob uses the hash and calls IPFS for the file.您将文件的哈希发送给 Bob,Bob 使用该哈希并为该文件调用 IPFS。 The file is now downloaded at Bob's end.该文件现在已在 Bob 的末尾下载。 The issue here is that anyone who can get access to the hash will also be able to get access to the file.这里的问题是任何可以访问哈希的人也可以访问该文件。

Sharing Data on IPFS by Asymmetric Cryptography通过非对称加密在 IPFS 上共享数据

Let' say you uploaded a file to IPFS and you want to share it only with Bob.假设您将文件上传到 IPFS,并且只想与 Bob 共享。

  • Bob will send you a public key. Bob 会向您发送一个公钥。 you will encrypt the file with Bob's public key and then upload it to IPFS network.您将使用 Bob 的公钥加密文件,然后将其上传到 IPFS 网络。

  • You send the hash of the file to Bob.您将文件的哈希值发送给 Bob。 Bob uses this hash and gets the file. Bob 使用此散列并获取文件。

  • Bob decrypts the file using his private key of the public key that was used to encrypt the file. Bob 使用他用来加密文件的公钥的私钥来解密文件。

In Asymmetric Cryptography, public keys are generated by the private key and if you lock something with a public key, the only key that will unlock that thing is the private key that the given public key is generated from.在非对称密码术中,公钥由私钥生成,如果您用公钥锁定某物,则唯一可以解锁该事物的密钥是生成给定公钥的私钥。

The better way of dealing with files on the blockchain is to use some sort of file storage services like AWS-S3, IPFS, Swarm, etc.在区块链上处理文件的更好方法是使用某种文件存储服务,如AWS-S3、IPFS、Swarm 等。

You can upload the files to one of the above file storage servers and generate the hash key(Which is used to access the file), and keep this key in the blockchain.您可以将文件上传到上述文件存储服务器之一并生成 hash 密钥(用于访问文件),并将此密钥保存在区块链中。

The advantages of using this method are -使用这种方法的优点是 -

  1. Low-cost solution低成本解决方案
  2. Easy and fast access to files using file storage searching algorithms使用文件存储搜索算法轻松快速地访问文件
  3. Lightweight轻的
  4. Flexibility to move from blockchain to DB or vice-a-versa从区块链迁移到数据库的灵活性,反之亦然
  5. If the file storage system has good security from tampering, then be assured as these files will not be accessed without the right hash key如果文件存储系统具有良好的防篡改安全性,那么请放心,如果没有正确的 hash 密钥,这些文件将不会被访问
  6. Easy to perform migration of file storage from one service to another轻松将文件存储从一项服务迁移到另一项服务

You can store images on Ethereum blockchain, but it is too expensive because of "blockspace premium" of high quality blockchain.您可以将图像存储在以太坊区块链上,但由于高质量区块链的“区块空间溢价”,它太贵了。

Other, more affordable decentralised storage solutions include其他更实惠的去中心化存储解决方案包括

  • Chia 正大
  • Filecoin (where Filecoin VM smart contracts can manipulate files) Filecoin(Filecoin VM 智能合约可以在其中操作文件)
  • Arweave Arweave
  • Storj斯托杰

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM