<p>Hello!</p>
<p>I already asked a few questions about go here a few days ago and since people were very kind and helpful, I thought I could ask here again. I really can't find much information or "how-to"s for Go in general and this wasn't anywhere to find.</p>
<p>I'd like to store images (PNGs, JPEGs, etc.) in a database with a random name and serve them on request. So if there's a request by a user to, let's say /image/h25Jkix, I want the server to serve image h25Jkix.jpg /.png.
So 2 questions came up:</p>
<ol>
<li><p>Can you think of a better way to store images than storing them in a database?</p></li>
<li><p>How would I achieve something like this? How would I insert the image into an HTML page or return just the image via an API? (I'm using net/http and Gorilla Mux at the moment; project can be found <a href="https://github.com/mkocs/imgcat">here</a>)</p></li>
</ol>
<p>Thanks in advance! :)</p>
<p>ps: if someone could point me at a good source for tutorials, etc. for Go, i'd be very happy!</p>
<hr/>**评论:**<br/><br/>kurin: <pre><blockquote>
<p>Can you think of a better way to store images than storing them in a database?</p>
</blockquote>
<p>A file system.</p></pre>clofresh: <pre><p>Agreed. Images on a file system + nginx with sendfile enabled is probably the fastest you'll be able to get</p></pre>lastrites17: <pre><p>Options include:</p>
<ul>
<li><p>Just use the net/http FileServer, by writing uploaded images to the filesystem</p></li>
<li><p>Use AWS with S3. <a href="https://github.com/mitchellh/goamz" rel="nofollow">Libraries</a> for go already exist </p></li>
</ul></pre>mko31: <pre><p>Thank you for taking the time to answer :)</p>
<p>What exactly would be the benefits of using S3 over just the net/http fileserver?</p></pre>lastrites17: <pre><p>Sorry for the delay in responding.</p>
<p>I would say the main advantage is the vast and cheap storage space. I think that's why it's relevant how many images we're talking about. For a small number, serving them up from your application server is just fine, but that presents scaling issues, since your server may not be optimized for storage. S3 scales well, and even better, can be scaled programatically as usage increases.</p></pre>eviltofu: <pre><p>Please specify how many images you intend to serve.</p></pre>mko31: <pre><p>I think I don't really understand what you mean. I want to allow users to upload images and show them to other people just by sending them a link.</p></pre>Yojihito: <pre><p>He means the volume of images you expect = how much traffic.</p>
<p>1.000 - 10.000? Install it on your desktop pc.
1.000.000? Maybe think about a server.</p></pre>mko31: <pre><p>Oh ok. Well I don't really know what to expect, but since I want to offer this service to everyone, I would expect a huge amount of traffic, so more like 1.000.000.</p></pre>Yojihito: <pre><p>Then you need a server. And using S3 seems to be the best solution, it scales well (afaik?) and you don't have to worry about the space.</p>
<p>Hosting this on your own computers ... well, not a good idea.</p></pre>mko31: <pre><p>Could you elaborate on how this works? I'm basically storing my data on a remote server?
I know that storing the images on my own computers might be a bad idea, but I didn't want to rely on external services.</p></pre>Yojihito: <pre><p>You program your image serving tool, deploy it on a server that has a decent internet connection (100Mb down + 100up) to handle the millions of connections every month and S3 to store the images and to offer fast delivery, nobody wants to wait 5 seconds for a picture to load.</p>
<p>Without external service? Won't work unless you have a solid workstation, a dedicated 100+ Mb symmetric connection and the space to save the images ..... expensive, damn expensive.</p>
<p>Of course that's only if you want to open your service for the whole world. With a limited user base around ~1000-10.000 it's possible to do it from home.</p></pre>mko31: <pre><p>But let's say I don't want to use this service in production, but rather just want to develop it for now and see what's possible later. There's gotta be a way to just store a smaller amount of data on my own devices and access it and later transfer it to a server.
It all seemed quite easy with NodeJS, but since I have no real idea about Go yet, it doesn't come as easy.</p></pre>Yojihito: <pre><p>Well in this case you can just deploy the executable you get after compiling your service on your machine and start it.</p>
<p>Datastorage could be anywhere you want, you could even give it as a parameter on program start, but for testing purpose I would hardcode it in the beginning (maybe bad practice but I only program in my spare time so I don't know better).</p>
<p>On Windows you would just start the .exe file and surf to the website your server program delivers.</p></pre>eviltofu: <pre><p>Also, if you only have under 100 small images, you could get away with caching them all in ram. </p></pre>pierrrre: <pre><p><a href="https://github.com/pierrre/imageserver" rel="nofollow">https://github.com/pierrre/imageserver</a> ?</p>
<p>I'm currently working on:</p>
<ul>
<li><p>native Go image instead of external process <a href="https://github.com/pierrre/imageserver/tree/feature/native" rel="nofollow">https://github.com/pierrre/imageserver/tree/feature/native</a></p></li>
<li><p>groupcache <a href="https://github.com/pierrre/imageserver/tree/feature/groupcache" rel="nofollow">https://github.com/pierrre/imageserver/tree/feature/groupcache</a></p></li>
</ul>
<p>You can store your images in a directory or use Amazon S3.</p></pre>sharptierce: <pre><p><a href="https://github.com/VoycerAG/gridfs-image-server" rel="nofollow">https://github.com/VoycerAG/gridfs-image-server</a> does this help?</p></pre>mko31: <pre><p>It could be helpful. I'll look into it. Thank you very much :)</p></pre>Iggymacd: <pre><p>You could have a look at <a href="https://camlistore.org/" rel="nofollow">https://camlistore.org/</a></p>
<p>More functionality than you need but there are likely things that would interest you from that project. </p></pre>flyingfryingpan: <pre><p>Have a look at this project by imgur: <a href="https://github.com/Imgur/mandible" rel="nofollow">https://github.com/Imgur/mandible</a></p>
<p>It is written in Go, has a REST API and you can choose from different storage backends (filesystem, s3, etc).</p></pre>klaaax: <pre><blockquote>
<p>Can you think of a better way to store images than storing them in a database?</p>
</blockquote>
<p>use amazon S3. </p></pre>mko31: <pre><p>Wouldn't using S3 mean storing the images on a remote server? Because I don't think that's an option. I want to store the images somewhere locally.</p></pre>dlsniper: <pre><p>Why is this not an option? Please give more details. </p></pre>mko31: <pre><p>I didn't plan to rely on external services and I have never used anything like this. If you could explain to me how S3 works, that would be great :)</p></pre>dlsniper: <pre><p><a href="http://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html" rel="nofollow">http://docs.aws.amazon.com/AmazonS3/latest/dev/Welcome.html</a></p></pre>dlsniper: <pre><p>S3 is not a CDN. CloudFront is a CDN, S3 should be used for storage, not distribution... </p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
0 回复
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传