画像 (それぞれ約 200 kB) を一括でアップロードしたいと考えています。CarrierWave、ペーパークリップなど、複数のオプションがあります。これらのアップロードを一括で実行するにはどうすればよいですか?
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;DR
しないでください。Rails は一括アップロード用に最適化されていないため、できる限り帯域外で行ってください。
FTP/SFTP を使用する
大量のファイルを処理する最善の方法は、Rails プロセスを拘束するのではなく、完全に帯域外プロセスを使用することです。たとえば、FTP、FTPS、SCP、または SFTP を使用してファイルを一括アップロードします。ファイルがサーバーに配置されたら、cron ジョブで後処理するか、inotify を使用して rake タスクを開始します。
注意: この手法を使用する場合は、ファイルのロックに注意してください。
キューを使用する
Rails 経由で行うことに固執する場合は、何百ものファイルをアップロードしないでください。代わりに、バックグラウンド ジョブまたはキューによって処理されるファイルを含む単一のアーカイブをアップロードします。ここには、SidekiqやRabbitMQなど、多くの代替手段があります。
アーカイブがアップロードされ、キューに入れられたジョブがサブミットされると、キュー プロセスはアーカイブをアンパックし、その他必要なことを実行できます。このタイプのソリューションは非常にうまく拡張できます。