Method: Fog::Storage::AWS::Mock#put_object

Defined in:
lib/fog/aws/requests/storage/put_object.rb

#put_object(bucket_name, object_name, data, options = {}) ⇒ Object

[View source]

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/fog/aws/requests/storage/put_object.rb', line 50

def put_object(bucket_name, object_name, data, options = {})
  acl = options['x-amz-acl'] || 'private'
  if !['private', 'public-read', 'public-read-write', 'authenticated-read'].include?(acl)
    raise Excon::Errors::BadRequest.new('invalid x-amz-acl')
  else
    self.data[:acls][:object][bucket_name] ||= {}
    self.data[:acls][:object][bucket_name][object_name] = self.class.acls(acl)
  end

  data = Fog::Storage.parse_data(data)
  unless data[:body].is_a?(String)
    data[:body] = data[:body].read
  end
  response = Excon::Response.new
  if (bucket = self.data[:buckets][bucket_name])
    response.status = 200
    object = {
      :body             => data[:body],
      'Content-Type'    => options['Content-Type'] || data[:headers]['Content-Type'],
      'ETag'            => Digest::MD5.hexdigest(data[:body]),
      'Key'             => object_name,
      'Last-Modified'   => Fog::Time.now.to_date_header,
      'Content-Length'  => options['Content-Length'] || data[:headers]['Content-Length'],
      'StorageClass'    => options['x-amz-storage-class'] || 'STANDARD',
      'VersionId'       => bucket[:versioning] == 'Enabled' ? Fog::Mock.random_base64(32) : 'null'
    }

    for key, value in options
      case key
      when 'Cache-Control', 'Content-Disposition', 'Content-Encoding', 'Content-MD5', 'Expires', /^x-amz-meta-/
        object[key] = value
      end
    end

    if bucket[:versioning]
      bucket[:objects][object_name] ||= []

      # When versioning is suspended, putting an object will create a new 'null' version if the latest version
      # is a value other than 'null', otherwise it will replace the latest version.
      if bucket[:versioning] == 'Suspended' && bucket[:objects][object_name].first['VersionId'] == 'null'
        bucket[:objects][object_name].shift
      end

      bucket[:objects][object_name].unshift(object)
    else
      bucket[:objects][object_name] = [object]
    end

    response.headers = {
      'Content-Length'   => object['Content-Length'],
      'Content-Type'     => object['Content-Type'],
      'ETag'             => object['ETag'],
      'Last-Modified'    => object['Last-Modified'],
    }

    response.headers['x-amz-version-id'] = object['VersionId'] if object['VersionId'] != 'null'
  else
    response.status = 404
    raise(Excon::Errors.status_error({:expects => 200}, response))
  end
  response
end