LoginSignup
0
0

More than 5 years have passed since last update.

AWS SDK for Ruby V2 でニフティクラウド オブジェクトストレージの API を叩く

Last updated at Posted at 2017-02-15

たまに触るとやり方忘れてるのでメモしておきます。

準備

Dockerfile 作成。

$ vi Dockerfile
FROM ruby:alpine
RUN gem install aws-sdk
CMD ruby /app.rb

スクリプト作成。

$ vi app.rb
require "aws-sdk"

client = Aws::S3::Client.new(
  endpoint: ENV["S3_ENDPOINT"],
  signature_version: ENV["S3_SIGNATURE_VERSION"],
)

client.list_buckets.buckets.each do |bucket|
  p bucket
end

設定ファイル作成。
S3_SIGNATURE_VERSION に s3 を指定するのがポイント。
AWS_REGION はなんでもよい。

$ vi .env
AWS_REGION=jp-east-2
AWS_ACCESS_KEY_ID=<Your Access Key Id>
AWS_SECRET_ACCESS_KEY=<Your Secret Access Key>
S3_ENDPOINT=https://jp-east-2.os.cloud.nifty.com
S3_SIGNATURE_VERSION=s3

実行

docker イメージビルド。

$ docker build -t s3cmd-ncos .
Sending build context to Docker daemon 4.096 kB
Step 1/3 : FROM ruby:alpine
 ---> 48de60a8463d
Step 2/3 : RUN gem install aws-sdk
 ---> Using cache
 ---> a853fa28ebd6
Step 3/3 : CMD ruby /app.rb
 ---> Using cache
 ---> b035a2f39011
Successfully built b035a2f39011

コンテナ実行。

$ docker run --rm --env-file .env -v $(pwd)/app.rb:/app.rb s3cmd-ncos
#<struct Aws::S3::Types::Bucket name="bucketa", creation_date=2016-07-17 06:27:58 UTC>
#<struct Aws::S3::Types::Bucket name="bucketb", creation_date=2017-02-13 01:53:51 UTC>
#<struct Aws::S3::Types::Bucket name="bucketc", creation_date=2016-12-07 09:39:18 UTC>
0
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
0
0