Module: S3::Client::API::Storage

Defined in:
lib/s3/client/api/storage.rb

Defined Under Namespace

Classes: Import, MultipartUpload

Instance Method Summary collapse

Instance Method Details

#bucketsObject



8
9
10
# File 'lib/s3/client/api/storage.rb', line 8

def buckets
  execute_storage(RestParameter.new(:get, '/'))
end

#create_bucket(bucket, options = {}) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/s3/client/api/storage.rb', line 34

def create_bucket(bucket, options = {})
  resource = '/'

  options = options.merge(bucket: bucket, content_type: 'application/xml')
  execute_storage(RestParameter.new(:put, resource, options)) do
    root = REXML::Element.new('CreateBucketConfiguration')
    root.add_attribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/')
    child = REXML::Element.new('LocationConstraint')
    child.add_text(@location)
    root.add_element(child)
    root
  end
end

#create_multipart_object(bucket, object_name, options = {}, &block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/s3/client/api/storage.rb', line 57

def create_multipart_object(bucket, object_name, options = {}, &block)
  mu = MultipartUpload.new(bucket, object_name, options) do
    self
  end

  # Initiate Multipart Upload
  upload_id = mu.initiate_multipart_upload

  begin
    # Upload Part
    upload_objects = mu.upload_part(upload_id, &block)

    # Complete Multipart Upload
    mu.complete_multipart_upload(upload_id, upload_objects)

  rescue => e
    # Abort Multipart Upload
    mu.abort_multipart_upload(upload_id)

    raise e
  end
end

#create_object(bucket, object_name, options = {}, &block) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/s3/client/api/storage.rb', line 48

def create_object(bucket, object_name, options = {}, &block)
  resource = "/#{object_name}"

  type = MIME::Types.type_for(object_name).first
  content_type = type ? type.to_s : 'application/octet-stream'
  options = options.merge(bucket: bucket, content_type: content_type)
  execute_storage(RestParameter.new(:put, resource, options), &block)
end

#delete_bucket(bucket) ⇒ Object



91
92
93
94
# File 'lib/s3/client/api/storage.rb', line 91

def delete_bucket(bucket)
  resource = '/'
  execute_storage(RestParameter.new(:delete, resource, bucket: bucket))
end

#delete_object(bucket, object) ⇒ Object



96
97
98
99
# File 'lib/s3/client/api/storage.rb', line 96

def delete_object(bucket, object)
  resource = "/#{object}"
  execute_storage(RestParameter.new(:delete, resource, bucket: bucket, content_type: 'application/json'))
end

#get_object(bucket, object, range = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/s3/client/api/storage.rb', line 80

def get_object(bucket, object, range = nil)
  resource = "/#{object}"
  headers = {}
  if range
    bt = "bytes=#{range.first}-"
    bt += "#{range.last}" if range.last != -1
    headers[:Range] = bt
  end
  execute_storage(RestParameter.new(:get, resource, bucket: bucket, raw_data: true, headers: headers))
end

#import(db_name, tbl_name, file_paths, options = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/s3/client/api/storage.rb', line 101

def import(db_name, tbl_name, file_paths, options = {})
  _import = Import.new(db_name, tbl_name, file_paths, options) do
    self
  end

  # calc label suffix => Fixnum
  suffix = _import.calc_label_suffix

  # import execute
  upload_objects = _import.execute(suffix)

  STDERR.puts "finished upload #{upload_objects.size} objects."
  STDERR.puts
  STDERR.puts 'upload_objects:'
  upload_objects.each do |o|
    STDERR.puts o
  end
end

#objects(bucket, prefix: nil, max: nil, marker: nil, delimiter: nil) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/s3/client/api/storage.rb', line 12

def objects(bucket, prefix: nil, max: nil, marker: nil, delimiter: nil)
  resource = '/'
  query_params = {}
  if prefix
    query_params.merge!('prefix' => prefix)
  end

  if max
    query_params.merge!('max-keys' => max)
  end

  if marker
    query_params.merge!('marker' => marker)
  end

  if delimiter
    query_params.merge!('delimiter' => delimiter)
  end

  execute_storage(RestParameter.new(:get, resource, bucket: bucket, query_params: query_params))
end