Class: Fog::Aliyun::Storage::File

Inherits:
Model
  • Object
show all
Defined in:
lib/fog/aliyun/models/storage/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#directoryObject

Returns the value of attribute directory.



34
35
36
# File 'lib/fog/aliyun/models/storage/file.rb', line 34

def directory
  @directory
end

Instance Method Details

#bodyObject



21
22
23
24
25
26
27
28
# File 'lib/fog/aliyun/models/storage/file.rb', line 21

def body
  attributes[:body] ||=
    if last_modified
      collection.get(identity).body
    else
      ''
    end
end

#body=(new_body) ⇒ Object



30
31
32
# File 'lib/fog/aliyun/models/storage/file.rb', line 30

def body=(new_body)
  attributes[:body] = new_body
end

#copy(target_directory_key, target_file_key, options = {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fog/aliyun/models/storage/file.rb', line 36

def copy(target_directory_key, target_file_key, options = {})
  requires :directory, :key
  source_bucket, directory_key = collection.check_directory_key(directory.key)
  source_object = if directory_key == ''
                    key
                  else
                    directory_key + '/' + key
                  end
  target_bucket, target_directory_key = collection.check_directory_key(target_directory_key)
  target_object = if target_directory_key == ''
                    target_file_key
                  else
                    target_directory_key + '/' + target_file_key
                  end
  service.copy_object(source_bucket, source_object, target_bucket, target_object, options)
  target_directory = service.directories.new(key: target_directory_key)
  target_directory.files.get(target_file_key)
end

#destroyObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/fog/aliyun/models/storage/file.rb', line 55

def destroy
  requires :directory, :key
  bucket_name, directory_key = collection.check_directory_key(directory.key)
  object = if directory_key == ''
             key
           else
             directory_key + '/' + key
           end
  service.delete_object(object, bucket: bucket_name)
  true
end

#metadataObject



67
68
69
# File 'lib/fog/aliyun/models/storage/file.rb', line 67

def 
  attributes[:metadata] ||= 
end

#owner=(new_owner) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/fog/aliyun/models/storage/file.rb', line 71

def owner=(new_owner)
  if new_owner
    attributes[:owner] = {
      display_name: new_owner['DisplayName'],
      id: new_owner['ID']
    }
  end
end

#public=(new_public) ⇒ Object



80
81
82
# File 'lib/fog/aliyun/models/storage/file.rb', line 80

def public=(new_public)
  new_public
end

#public_urlObject



106
107
108
109
# File 'lib/fog/aliyun/models/storage/file.rb', line 106

def public_url
  requires :key
  collection.get_url(key)
end

#save(options = {}) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/fog/aliyun/models/storage/file.rb', line 111

def save(options = {})
  requires :body, :directory, :key
  options['Content-Type'] = content_type if content_type
  options['Content-Disposition'] = content_disposition if content_disposition
  options.merge!()
  bucket_name, directory_key = collection.check_directory_key(directory.key)
  object = if directory_key == ''
             key
           else
             directory_key + '/' + key
           end
  if body.is_a?(::File)
    service.put_object(object, body, options.merge(bucket: bucket_name))
    data = service.head_object(object, bucket: bucket_name)
  elsif body.is_a?(String)
    data = service.put_object_with_body(object, body, options.merge(bucket: bucket_name)).data
  else
    raise Fog::Aliyun::Storage::Error, " Forbidden: Invalid body type: #{body.class}!"
  end
  update_attributes_from(data)
  

  self.content_length = Fog::Storage.get_body_size(body)
  self.content_type ||= Fog::Storage.get_content_type(body)
  true
end

#url(expires, options = {}) ⇒ String

Get a url for file.

required attributes: directory, key

Parameters:

  • expires (String)

    number of seconds (since 1970-01-01 00:00) before url expires

  • options (Hash) (defaults to: {})

Returns:

  • (String)

    url



92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/fog/aliyun/models/storage/file.rb', line 92

def url(expires, options = {})

  expires = expires.nil? ? 0 : expires.to_i

  requires :directory, :key
  bucket_name, directory_key = collection.check_directory_key(directory.key)
  object = if directory_key == ''
             key
           else
             directory_key + '/' + key
           end
  service.get_object_http_url_public(object, expires, options.merge(bucket: bucket_name))
end