AWS S3 をコマンドラインインタフェース(CLI)で操作するまでの手順

AWS S3をCLIで操作するまでの一連の手順が書いてある記事を見つけられなかったのでここにまとめます。わからない用語はその都度調べてみてください。

開発環境
OS X 10.10.2 Yosemite

1. AWSのアカウントを取得する

[ 参考サイト ]

AWS アカウント作成の流れ | アマゾン ウェブ サービス (AWS日本語)

2. S3 でバケットを作る

[ 参考サイト ]

Amazon S3 編。S3 バケットの作成をご説明 | 使い方・ナレコムAWSクラウド | ナレコムAWSレシピ

はじめてのAWS、“つまずきポイント”はココだ! - [3]S3のつまずきポイント、命名ルール知らないとエラーに:ITpro

3. IAMでAccess_keyとSecret_keyを取得してPolicyを設定する

取得したidは必ずメモしておいてください。

[ 参考サイト ]

awsのs3を操作する為のaccess keyとsecret keyを取得する(IAM)

4. ターミナルからawscliをインストール

$ sudo pip install awscli

※ pip が入っていなければインストールしてください。

[ 参考サイト ]

Pythonでsetuptools, pipを使ってパッケージ管理する方法 - hikm's blog

5. AWS CLI の設定

 $ aws configure

[ 参考サイト ]

AWS Tips:AWS APIでAWSの操作を自動化する - @IT

region は "Tokyo" ではなく、"ap-northeast-1" と設定するので注意。 output format は空欄のまま Enter を押して問題ないです。

6. 動作テスト

$ aws s3 ls
2015-02-24 12:07:27 "作成したバケット名"

となればOK。時間のところは作成時間が表示されるので、違っていても問題ありません。

その後の AWS S3 のCLIのコマンドは、こちらのサイトでわかりやすく解説してくれています。大体はLinuxコマンドと同じようなので安心です。

AWS S3のCLI(コマンドラインインターフェース)を試してみる | もふもふ技術部

NoMethodError undefined method 'title' の対処 rake db:migrate:reset

開発環境
OS X 10.9.5
Ruby 2.1.3
Rails 4.2.0

articles/new にアクセスしたところ

undefined method `title' 

というNoMethodErrorが出た。

結論は、「テーブル構造を確認しましょう」です。

環境は

# app/view/articles/new.html.slim

.container
  .row
    .col-md-12
      .box
        = render partial: 'articles/form', locals: { article: @article }
# app/view/articles/_form.html.slim

= form_for article, html: { multipart: true, class: 'form-horizontal' } do |f|
  - if article.errors.any?
    - article.errors.full_messages.each do |message|
      .alert.alert-danger= message

  .form-group
    = f.label :title, class: 'control-label col-md-2'
    .col-md-10
      = f.text_field :title, placeholder: Article.human_attribute_name(:title), required: true,
        class: 'form-control input-lg'

   # 中略

 .clearfix
    = f.submit t('save'), class: 'btn btn-default pull-right'
# app/controller/articles_controller.rb

class ArticlesController < ApplicationController
  
  def new
    @article = Article.new
  end

   # 中略

end

ググってもそれらしき情報が見つからず。。 何を思ったか、ふとdbのテーブル構造に着目した。

# db/migrate/20150218041435_create_articles.rb

class CreateArticles < ActiveRecord::Migration
  def change
    create_table :articles do |t|
      t.string :title, null: false
      t.string :image, null: false
      t.text :body

      t.timestamps
    end
   end
end

ここはこうなっているのに、

mysql > desc articles;
 Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | int(11)      | NO   | PRI | NULL    | auto_increment |
| created_at | datetime     | NO   |     | NULL    |                |
| updated_at | datetime     | NO   |     | NULL    |        

「なんで title と image と body のカラムがないんだ...!!」

ググったところ

$ rake db:reset

このコマンドは、db/schema.rb に指定されたテーブル設計にリセットするコマンドらしいのだが、Articleモデルを作った時はカラムの設定をしておらず、後から 2015**.rb に付け加えていたので

# db/schema.rb

ActiveRecord::Schema.define(version: 20150218041435) do

  create_table "articles", force: :true do |t|
    
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

end

schema.rbはこのようになっていた。

つまり、「最初はとりあえず rails g model Article -d mysql で作って、カラムは後から付けたそう。」と考えていたにもかかわらず

$ rake db:migrate:reset

とすべきところを

$ rake db:reset

とやって、後から付け足したカラムがしっかり上書きされた気になっていたのが問題だった。

この手のエラーはググってもヒットしにくいのでここに書いておきます。参考になれば。

[ 参考サイト ]

rake db:reset と rake db:migrate:reset の違い | EasyRamble

ruby extconf.rb でのエラー

開発環境
OS X 10.9.5
Ruby 2.1.3
Rails 4.2.0
Vagrant 1.7.2
CentOS 6.5
$ bundle install --path vendor/bundle

をした際に

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    /home/vagrant/.rbenv/versions/2.1.3/bin/ruby extconf.rb 
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/vagrant/.rbenv/versions/2.1.3/bin/ruby
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /vagrant/okome/vendor/bundle/ruby/2.1.0/gems/mysql2-0.3.17 for inspection.
Results logged to /vagrant/okome/vendor/bundle/ruby/2.1.0/extensions/x86_64-linux/2.1.0-static/mysql2-0.3.17/gem_make.out
An error occurred while installing mysql2 (0.3.17), and Bundler cannot continue.
Make sure that `gem install mysql2 -v '0.3.17'` succeeds before bundling.

というエラーが出た。最後のところにmysql2を入れろと書いてあったので、

$ gem install mysql2 -v '0.3.17'

を実行したところ

ERROR:  Error installing mysql2:
    ERROR: Failed to build gem native extension.

    /home/vagrant/.rbenv/versions/2.1.3/bin/ruby extconf.rb
checking for ruby/thread.h... yes
checking for rb_thread_call_without_gvl() in ruby/thread.h... yes
checking for rb_thread_blocking_region()... yes
checking for rb_wait_for_single_fd()... yes
checking for rb_hash_dup()... yes
checking for rb_intern3()... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lm... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lz... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lsocket... no
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lnsl... yes
checking for mysql_query() in -lmysqlclient... no
checking for main() in -lmygcc... no
checking for mysql_query() in -lmysqlclient... no
*** extconf.rb failed ***
Could not create Makefile due to some reason, probably lack of necessary
libraries and/or headers.  Check the mkmf.log file for more details.  You may
need configuration options.

Provided configuration options:
    --with-opt-dir
    --without-opt-dir
    --with-opt-include
    --without-opt-include=${opt-dir}/include
    --with-opt-lib
    --without-opt-lib=${opt-dir}/lib
    --with-make-prog
    --without-make-prog
    --srcdir=.
    --curdir
    --ruby=/home/vagrant/.rbenv/versions/2.1.3/bin/ruby
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysql-config
    --without-mysql-config
    --with-mysql-dir
    --without-mysql-dir
    --with-mysql-include
    --without-mysql-include=${mysql-dir}/include
    --with-mysql-lib
    --without-mysql-lib=${mysql-dir}/lib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mlib
    --without-mlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-zlib
    --without-zlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-socketlib
    --without-socketlib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-nsllib
    --without-nsllib
    --with-mysqlclientlib
    --without-mysqlclientlib
    --with-mygcclib
    --without-mygcclib
    --with-mysqlclientlib
    --without-mysqlclientlib

extconf failed, exit code 1

Gem files will remain installed in /home/vagrant/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/mysql2-0.3.17 for inspection.
Results logged to /home/vagrant/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/extensions/x86_64-linux/2.1.0-static/mysql2-0.3.17/gem_make.out

最後の方は変わったが、どうやら元凶は ruby extconf.rb らしい。

$ sudo yum -y install mysql-devel

これで解決。

参考サイト

gem インストール時に発生したエラーとその解決方法まとめ - kzy52's blog

Warning: you should require 'minitest/autorun' instead. Warning: or add 'gem "minitest"' before 'require "minitest/autorun"' エラーの対処

開発環境
OS X 10.9.5
Ruby 2.1.3
Rails 4.2.0
Vagrant 1.7.2
CentOS 6.5

第6章 ユーザーのモデルを作成する | Rails チュートリアル

ここでRSpecでテストをした時のエラー

$ bundle exec rspec spec/models/example.rb

 

Warning: you should require 'minitest/autorun' instead.

Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'

ググったところ、spec/spec_helper.rbに

require 'minitest/autorun'

  を追記。   これでもだめなら、Gemfileに

gem 'shoulda-matchers', '~> 2.6.0'

を追記。

$ bundle update

$ bundle install

をした後に再度

$ bundle exec rspec spec/models/example.rb

これで

Finished in 0.00037 seconds
1 example, 0 failures, 1 pending

Randomized with seed 54265

Run options: --seed 52463

# Running:

このようなメッセージがでれば解決。

[参考サイト]

RSpecでテストする際に、「Warning: you should require 'minitest/autorun' instead. Warning: or add 'gem "minitest"' before 'require "minitest/autorun"'」と警告された。 - 機械音痴な情報系

Warning regarding minitest/unit when used with rails 4.1.0.beta1 · Issue #408 · thoughtbot/shoulda-matchers · GitHub

Railsの勉強 初手からの展望

以下のサイトを参考にして勉強します。

 

Rubyの基礎文法は


Ruby入門 (全23回) - プログラミングならドットインストール

 


はじめてのRuby | CODEPREP -オンラインでプログラミングを学ぼう-

 

Railsの基礎は

 


Ruby on Rails 4入門 (全28回) - プログラミングならドットインストール

 

Railsをさらに深めるために


Ruby on Rails チュートリアル:実例を使って Rails を学ぼう

 

Ruby on Rails Guides

この2つを繰り返します。最後のは全て英語です。

 

以前CakePHPをかじっていましたが、Cakeと比べてRailsはネットに落ちている情報が多いなぁと感じています。チュートリアルもやや踏み込んでいてありがたいです。

頑張るぞい。

 

 

Ruby on Rails、始めました。

環境は

OS X 10.9.5

Ruby 2.1.3

Rails 4.2.0

Vagrant 1.7.2

CentOS 6.5

です。

 

開発環境設定で役立ったサイト

Vagrant で Ruby on Rails の環境を構築するまでの手順 - Qiita

rails console が動かない - 同じにやっても動かない

» Rails4.2beta1をインストールして最初にはまったこと TECHSCORE BLOG

今更ながらMacOSXにVagrant+VirtualBoxをインストールして使ってみた | 本日も乙

web帳 | VirtualBoxとVagrantで開発環境を構築

vagrant global-status --pruneでゾンビ仮想マシンを削除する - DQNEO起業日記

 

2月末までにキュレーションメディアを作ります。頑張るぞい。