CryptKeeper

Rails gem for connecting to the Google Storage API

Install

gem install cryptkeeper

Depending upon how your environment is set up, you may also need:

require 'crypt_keeper'

Usage

So far:

keeper = CryptKeeper::Connection.new({:access_key => 'your_access_key', :secret_key => 'your_secret_key'})

See all of my buckets

puts keeper.buckets.each do |bucket|
    puts bucket.name
    puts bucket.created_at
end

Search for all files that start with “Picture” The following returns meta info on each of the found objects within the specified bucket

metas = keeper.bucket_meta_objects('adam_first_bucket', {:prefix => 'Picture'})

Now it’s time to grab the actual object (data) I am writing it to Files here on my local machine

metas.each do |meta|
    puts meta.key # In my case, prints Picture 1.png
    f = File.new("/Users/user/Desktop/#{meta.key}",'w')
    # object method will go and fetch it unless it's been fetched before
    # if you want to force another trip to the Crypt, do meta.object!
    f.write(meta.object)
    f.close()
end

Creates 3 new picture files on my Desktop: Picture 1.png, Picture 2.png, Picture 3.png

Add a new object to a bucket

new_bucket = keeper.create_bucket({:name => "adam_new_bucket3"})

data = File.open("/Users/user/Desktop/Picture 3.png", "rb")
new_bucket.add_object('Picture 5.png', data, 'image/png')

data = File.open("/Users/user/Desktop/text_file.txt", 'r')
new_bucket.add_object("myTextFile.txt", data) # if you don't specify a content type, binary/octet-stream is used

Copy an object from one bucket to another

# Fetch an object
my_object = keeper.bucket_meta_objects('adam_new_bucket3', {:prefix => 'Picture%205.png'}).first

# Copy an object to another bucket
my_object.copy_to 'adam_first_bucket' # copy to a different bucket and keep the path intact
my_object.copy_to 'adam_new_bucket', 'path_1/path_2/path_3/Picture5.png' # copy to a different bucket and path

Delete an object

my_object.delete

Delete an object

keeper.bucket_meta_objects('adam_new_bucket3', {:prefix => 'Picture%205.png'}).first.delete