lib/fog/aliyun/models/storage/files.rb in fog-aliyun-0.3.10 vs lib/fog/aliyun/models/storage/files.rb in fog-aliyun-0.3.11
- old
+ new
@@ -1,13 +1,14 @@
# frozen_string_literal: true
require 'fog/core/collection'
require 'fog/aliyun/models/storage/file'
+require 'aliyun/oss'
module Fog
- module Storage
- class Aliyun
+ module Aliyun
+ class Storage
class Files < Fog::Collection
attribute :directory
attribute :limit
attribute :prefix, :aliases => 'Prefix'
attribute :path
@@ -15,11 +16,11 @@
attribute :delimiter, :aliases => 'Delimiter'
attribute :is_truncated, :aliases => 'IsTruncated'
attribute :marker, :aliases => 'Marker'
attribute :max_keys, :aliases => ['MaxKeys', 'max-keys']
- model Fog::Storage::Aliyun::File
+ model Fog::Aliyun::Storage::File
# check_directory_key have two functions:
# 1. trim the directory_key suffix '/'
# 2. checking whether the directory_key is a bucket.
# If so, it will return directly to avoid to create a new redundant folder named with directory_key.
@@ -46,16 +47,24 @@
end
end
return bucket_name, directory_key
end
- def all(_options = {})
+ def all(options = {})
requires :directory
bucket_name, directory_key = check_directory_key(directory.key)
- prefix_value = prefix
+ remap_attributes(options, {
+ :delimiter => 'delimiter',
+ :marker => 'marker',
+ :max_keys => 'max-keys',
+ :prefix => 'prefix'
+ })
+ prefix_value = options['prefix']
prefix_value = directory_key + '/' + prefix if directory_key != '' && directory_key != '.' && !directory_key.nil?
- files = service.list_objects(prefix: prefix_value, bucket: bucket_name)['Contents']
+ options['prefix'] = prefix_value
+ options['bucket'] = bucket_name
+ files = service.list_objects(options)['Contents']
return if files.nil?
data = []
i = 0
files.each do |file|
next unless file['Key'][0][-1] != '/'
@@ -100,67 +109,38 @@
else
directory_key + '/' + key
end
begin
data = service.get_object(object, nil, bucket: bucket_name)
- rescue StandardError => error
- case error.response.body
- when %r{<Code>NoSuchKey</Code>},%r{<Code>SymlinkTargetNotExist</Code>}
+ lastModified = data['headers'][:last_modified]
+ last_modified = (Time.parse(lastModified).localtime if !lastModified.nil? && lastModified != '')
+
+ date = data['headers'][:date]
+ date = (Time.parse(date).localtime if !date.nil? && date != '')
+ file_data = {
+ body: data[:body],
+ content_length: data['headers'][:content_length].to_i,
+ key: key,
+ last_modified: last_modified,
+ content_type: data['headers'][:content_type],
+ etag: data['headers'][:etag],
+ date: date,
+ connection: data['headers'][:connection],
+ accept_ranges: data['headers'][:accept_ranges],
+ server: data['headers'][:server],
+ object_type: data['headers'][:x_oss_object_type]
+ }
+
+ new(file_data)
+ rescue AliyunOssSdk::ServerError => error
+ case error.error_code
+ when %r{NoSuchKey},%r{SymlinkTargetNotExist}
nil
else
raise(error)
end
end
-
- contentLen = data[:headers]['Content-Length'].to_i
-
- if block_given?
- pagesNum = (contentLen + Excon::CHUNK_SIZE - 1) / Excon::CHUNK_SIZE
-
- for i in 1..pagesNum
- _start = (i - 1) * Excon::CHUNK_SIZE
- _end = i * Excon::CHUNK_SIZE - 1
- range = "#{_start}-#{_end}"
- begin
- data = service.get_object(object, range, bucket: bucket_name)
- chunk = data[:body]
- rescue StandardError => error
- case error.response.body
- when %r{<Code>NoSuchKey</Code>},%r{<Code>SymlinkTargetNotExist</Code>}
- chunk = ''
- else
- raise(error)
- end
- end
- yield(chunk)
- body = nil
- end
- else
- body = data[:body]
- end
-
- lastModified = data[:headers]['Last-Modified']
- last_modified = (Time.parse(lastModified).localtime if !lastModified.nil? && lastModified != '')
-
- date = data[:headers]['Date']
- date = (Time.parse(date).localtime if !date.nil? && date != '')
- file_data = {
- body: body,
- content_length: contentLen,
- key: key,
- last_modified: last_modified,
- content_type: data[:headers]['Content-Type'],
- etag: data[:headers]['ETag'],
- date: date,
- connection: data[:headers]['Connection'],
- accept_ranges: data[:headers]['Accept-Ranges'],
- server: data[:headers]['Server'],
- object_type: data[:headers]['x-oss-object-type'],
- content_disposition: data[:headers]['Content-Disposition']
- }
-
- new(file_data)
end
def get_url(key)
requires :directory
bucket_name, directory_key = check_directory_key(directory.key)
@@ -223,10 +203,10 @@
accept_ranges: data[:headers]['Accept-Ranges'],
server: data[:headers]['Server'],
object_type: data[:headers]['x-oss-object-type']
}
new(file_data)
- rescue Fog::Storage::Aliyun::NotFound
+ rescue Fog::Aliyun::Storage::NotFound
nil
end
def new(attributes = {})
requires :directory