lib/saviour/s3_storage.rb in saviour-0.5.2 vs lib/saviour/s3_storage.rb in saviour-0.5.3
- old
+ new
@@ -24,42 +24,46 @@
# http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html
if path.bytesize > 1024
raise(KeyTooLarge, "The key in S3 must be at max 1024 bytes, this key is too big: #{path}")
end
+ # TODO: Use multipart api
client.put_object(@create_options.merge(body: file_or_contents, bucket: @bucket, key: path))
end
def write_from_file(file, path)
file.rewind
write(file, path)
end
def read_to_file(path, dest_file)
+ path = sanitize_leading_slash(path)
+
dest_file.binmode
dest_file.rewind
dest_file.truncate(0)
- io = get_file_stringio(path)
- while data = io.read(1024 * 1024)
- dest_file.write(data)
- end
-
- dest_file.flush
+ client.get_object({ bucket: @bucket, key: path }, target: dest_file)
+ rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
+ raise FileNotPresent, "Trying to read an unexisting path: #{path}"
end
def read(path)
- get_file_stringio(path).read
+ path = sanitize_leading_slash(path)
+
+ client.get_object(bucket: @bucket, key: path).body.read
+ rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
+ raise FileNotPresent, "Trying to read an unexisting path: #{path}"
end
def delete(path)
path = sanitize_leading_slash(path)
client.delete_object(
bucket: @bucket,
- key: path,
+ key: path
)
end
def exists?(path)
path = sanitize_leading_slash(path)
@@ -98,20 +102,9 @@
cp(source_path, destination_path)
delete(source_path)
end
private
-
- def get_file_stringio(path)
- path = sanitize_leading_slash(path)
-
- client.get_object(
- bucket: @bucket,
- key: path
- ).body
- rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::NoSuchKey
- raise FileNotPresent, "Trying to read an unexisting path: #{path}"
- end
def public_url_prefix
if @public_url_prefix.respond_to?(:call)
@public_url_prefix.call
else