LoginSignup
1
0

More than 5 years have passed since last update.

asset_sync without Rails

Posted at

asset_syncをRailsアプリケーション以外から使います

手順

  1. Gemfileにasset_sync, fog-awsを記述
  2. Raketaskを作成
  3. Rakefileを作成
  4. Raketask実行

想定するディレクトリ構造

- project
 |- Gemfile
 |- Rakefile
 |- assets.rake
 |- public
   |- images
     |- hogehoge.png
     |- ...

1. Gemfileにasset_sync, fog-awsを記述

# プロジェクトルートにGemfileを設置します
$ touch Gemfile
$ vi Gemfile # 内容は以下
$ bundle install
ruby '2.3.0'

source 'https://rubygems.org' do
  gem 'asset_sync'
  gem 'fog-aws'
end

2. Raketaskを作成

assets.rake
# frozen_string_literal: true
require 'asset_sync'
require 'fog/aws'

namespace :assets do
  desc 'Synchronize assets to S3'
  task :sync do

    # ここでconfigを設定するのが筋がいいのかはよくわからない
    AssetSync.configure do |config|
      config.fog_provider = 'AWS'
      config.aws_iam_roles = true
      config.fog_region = 'ap-northeast-1'
      config.existing_remote_files = 'keep'
      config.fog_directory = 'your_s3_bucket_name'
      config.public_path = 'public/path'
      config.prefix = 'prefix/path'
      config.log_silently = false
    end

    puts AssetSync.sync
  end
end

prefixとpublic_pathについて

prefix : ローカル(コピー元)とS3(コピー先) 両方で assetが入っているディレクトリパス
public_path : ローカルのassetが置かれているディレクトリのパス

config.public_path = 'public'
config.prefix = 'assets'

という設定かつ、

-project
 |-public
   |- assets
      |- ここにsyncしたいファイルが入っている

というディレクトリ構造のとき、S3上では

-S3_bucket
 |- assets
    |- ここにsyncしたファイルが入る

3. Rakefileを作成

# frozen_string_literal: true
load "assets.rake"

4. Raketask実行

$ rake assets:sync

Docs

「Railsに乗っかっておけばDocs書かなくてもいいだろう」って初心者殺しだよね

Add public_path and prefix config options so asset_sync can be used outside Rails.

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0