LoginSignup
8
5

More than 5 years have passed since last update.

Gem Fogを利用したS3の操作覚書

Last updated at Posted at 2015-12-09

概要

aaa

Fogを利用してS3上のコネクション/ディレクトリ検索/ディレクトリ削除を行う
https://github.com/fog/fog

基本環境

  • Ruby on Rails 4.2
  • Fog(latest)

S3上のバケットとコネクションをはる

connection = Fog::Storage.new(
    provider:              'AWS',
    aws_access_key_id:     Your_aws_key,
    aws_secret_access_key: Your_aws_secret,
    region:                Your_aws_region
)
bucket = connection.directories.get('Your_s3_bucket')

s3内のディレクトリを検索

S3内は階層がないのでvalue検索をして取得を行う
http://dev.classmethod.jp/cloud/aws/amazon-s3-folders/

# tmp内のファイルを取得
# http://www.rubydoc.info/gems/fog/Fog%2FStorage%2FRackspace%2FFiles%3Aprefix
bucket.files.prefix = "tmp"


# ディレクトリ内で3日以前に作成されたファイルを取得
directories = []
bucket.files.all.each do |file|
  unless file.last_modified.to_date > 3.days.ago.to_date
    obj = file.key.match %r{tmp/[^ ]*/}
    directories << obj[0]
  end
end

 ファイルの削除

# 他にもdestroy方法色々:http://fog.io/about/getting_started.html
bucket.files.destroy

参考

The Ruby cloud services library
http://fog.io/about/getting_started.html

8
5
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
8
5