我想批量上传图片(每张大约 200 kB)。我们有多种选择,例如 CarrierWave、Paperclip 等。如何批量执行这些上传?
2 回答
Like other things in computer Sc, the answer is it depends™. What I really mean is
- End-Users going to be uploading these?. If yes use jQuery File Upload plugin to present an easy to use interface.
- For storage, you can storage it on your server. or even better, upload the images directly from users computers to amazon s3. here is an example of Uploading Files to S3 in Ruby with Paperclip
- Obviously you will need to convert this into background job where all images get images with ajax in separate jobs. if you already don't have a fav que system, I would suggest resque or sidekiq
Note: if you choose to upload images directly to S3 via CORS, then your rails server is free need from managing file uploads. And direct uploads are suggested if you have large images or large number of files to be uploaded.
However, direct uploads limit your ability to modify the images(resize etc). so keep that in mind if you are choosing the direct upload solution.
TL;博士
不。Rails 没有针对批量上传进行优化,所以尽可能在带外进行。
使用 FTP/SFTP
处理大量文件的最佳方法是使用完全带外的进程,而不是捆绑 Rails 进程。例如,使用 FTP、FTPS、SCP 或 SFTP 批量上传文件。文件在服务器上后,使用 cron 作业对它们进行后处理,或使用 inotify 启动 rake 任务。
注意:确保在使用此技术时注意文件锁定。
使用队列
如果您坚持通过 Rails 进行,请不要上传数百个文件。相反,上传包含要由后台作业或队列处理的文件的单个存档。这里有很多替代方案,包括Sidekiq和RabbitMQ等。
上传存档并提交排队的作业后,您的队列进程可以解压缩存档并执行其他任何需要完成的工作。这种类型的解决方案可以很好地扩展。