elikem
6/9/2016 - 1:25 AM

Basics for RiakCS file handling using fog

Basics for RiakCS file handling using fog

# Register connection
connection = Fog::Storage.new(
  provider:               "AWS",
  aws_access_key_id:      "YOUR-RIAK-CS-ACCESS-KEY",
  aws_secret_access_key:  "YOUR-RIAK-CS-SECRET_KEY",
  host:                   "s3.amazonaws.com",
  scheme:                 "http",
  aws_signature_version: 	"2",
  connection_options: {
    proxy: "http://127.0.0.1:8080",
  }
)

# Local connection (dev/test environment)
connection = Fog::Storage.new({
  :provider   => 'Local',
  :local_root => '~/fog',
  :endpoint   => '~/fog'
})

# Create bucket
bucket = connection.directories.create(
  :key    => "test-bucket"
)

# Get bucket
bucket = connection.directories.get("test-bucket")

# Upload file to bucket
file = bucket.files.create(
  :key    => "data.json",
  :body   => File.open("data.json")
)

# Get file
file = bucket.files.get("data.json")

# Delete file from bucket
bucket.files.destroy("data.json")

# Move files from bucket to bucket (To move or rename a file, perform a copy operation and then delete the old file)
file.copy(new_bucket.key, file.key)
file.destroy

# Check if bucket exists
if bucket = connection.directories.get("test-bucket")
	# Do stuff with bucket variable
else
	# Raise exception or create bucket
end

# Check if file exists
unless bucket.files.head('data.json')
   #do something, like creating the file
   user bucket.files.head('data.json') as a variable
end