Class: CloudStorage

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudstorage.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ CloudStorage

Returns a new instance of CloudStorage.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/cloudstorage.rb', line 5

def initialize(opts = {})
  @client = Google::APIClient.new(
      :application_name => opts['application_name'],
      :application_version => opts['application_version']
  )

  key = Google::APIClient::PKCS12.load_key(
      opts['key'],
      "notasecret"
  )

  @client.authorization = Signet::OAuth2::Client.new(
      :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
      :audience => 'https://accounts.google.com/o/oauth2/token',
      :scope => "https://www.googleapis.com/auth/devstorage.full_control",
      :issuer => opts['service_email'],
      :signing_key => key
  )

  refresh_auth

  @cs = @client.discovered_api("storage", "v1beta2")

  @project = opts['project_id'] if opts['project_id']
  @project = opts['project'] if opts['project']
  @bucket = opts['bucket']
end

Instance Method Details

#bucketsObject



33
34
35
36
37
38
# File 'lib/cloudstorage.rb', line 33

def buckets
  resp = api({
    :api_method => @cs.buckets.list
  })
  items_to_array(resp['items'])
end

#create_bucket(bucket_name) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cloudstorage.rb', line 60

def create_bucket(bucket_name)
  bucket = get_bucket(bucket_name)
  return bucket if bucket
  resp = api({
        :api_method => @cs.buckets.insert,
        :body_object => {
            "name" => bucket_name
        }

    })
  "gs://#{bucket_name}/" unless resp['error']
end

#delete_bucket(bucket_name) ⇒ Object



73
74
75
76
77
78
79
80
# File 'lib/cloudstorage.rb', line 73

def delete_bucket(bucket_name)
  api({
      :api_method => @cs.buckets.delete,
      :parameters => {
          "bucket" => bucket_name
      }
  })
end

#delete_object(bucket_name, object_name) ⇒ Object



105
106
107
108
109
110
111
112
113
# File 'lib/cloudstorage.rb', line 105

def delete_object(bucket_name, object_name)
  api({
      :api_method => @cs.objects.delete,
      :parameters => {
          "bucket" => bucket_name,
          "object" => object_name
      }
  })
end

#get_bucket(bucket_name = @bucket) ⇒ Object



50
51
52
53
54
55
56
57
58
# File 'lib/cloudstorage.rb', line 50

def get_bucket(bucket_name = @bucket)
  resp = api({
    :api_method => @cs.buckets.get,
    :parameters => {
        'bucket' => bucket_name
    }
  })
  resp['error'] ? nil : "gs://#{bucket_name}/"
end

#list_bucket_contents(bucket_name = @bucket) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/cloudstorage.rb', line 40

def list_bucket_contents(bucket_name = @bucket)
  resp = api({
      :api_method => @cs.objects.list,
      :parameters => {
          "bucket" => bucket_name
      }
  })
  items_to_array(resp['items'])
end

#post_object(bucket_name, local_file, content_type, cs_file_name = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cloudstorage.rb', line 87

def post_object(bucket_name, local_file, content_type, cs_file_name = nil)
  cs_file_name ||= File.basename(local_file)
  bucket = create_bucket(bucket_name)
  if bucket.nil? then return "Unable to find or create bucket. Please check the bucket name and try again" end
  media = Google::APIClient::UploadIO.new(local_file, content_type)
  resp = api({
      :api_method => @cs.objects.insert,
      :media => media,
      :parameters => {
          "bucket" => bucket_name,
          "uploadType" => "multipart",
          "name" => cs_file_name
      },
      :body_object => {"contentType" => content_type}
  })
  "#{bucket}#{cs_file_name}"
end

#post_object_default_bucket(local_file, content_type, cs_file_name = nil) ⇒ Object



82
83
84
85
# File 'lib/cloudstorage.rb', line 82

def post_object_default_bucket(local_file, content_type, cs_file_name = nil)
  if @bucket.nil? then return "Default bucket not specified" end
  post_object(@bucket, local_file, content_type, cs_file_name)
end

#refresh_authObject



115
116
117
# File 'lib/cloudstorage.rb', line 115

def refresh_auth
  @client.authorization.fetch_access_token!
end