How can I get the size of an s3 object in fog without downloading it?
For example:
connection.get_object(dir.key, latest_backup.key).body.size
requires I download the object first.
How can I find out the size before downloading?
To find the size of the file do this:
connection.head_object(bucket_name, object_name, options = {})
And look for this in the object that comes back:
Content-Length
This will also give you other good information like the checksum in ETag and other things like that.
Reference: http://rubydoc.info/gems/fog/Fog/Storage/AWS/Real#head_object-instance_method
remote_file.content_length
does the trick for me (Fog 1.21.0)
where remote_file
is gotten from the remote directory, like so:
def connection
Fog::Storage.new(fog_config)
end
def directory
connection.directories.get(bucket)
end
def remote_file
remote_files = directory.files.last
end
module S3
def self.directory
connection.directories.new(key: ENV['AWS_BUCKET'])
end
def self.connection
Fog::Storage.new(
provider: 'AWS',
aws_access_key_id: ENV['AWS_ACCESS_KEY_ID'],
aws_secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: ENV['AWS_REGION']
)
end
def self.file(key)
directory.files.head(key)
end
end
file = S3.file(key)
file.content_length
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.