Created
August 12, 2018 01:08
-
-
Save tzmfreedom/08757e4df41b5e0b8d0ceef4b2bb5c36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
class S3Client | |
def initialize(bucket_name:) | |
@bucket_name = bucket_name | |
end | |
def download(key:, filepath: nil) | |
return unless exist_key?(key) | |
if filepath | |
bucket.object(key).download_file(filepath) | |
else | |
bucket.object(key).get.body.read | |
end | |
end | |
def upload(key:, filepath:) | |
return if exist_key?(key) | |
bucket.object(key).upload_file(filepath) | |
end | |
private | |
def exist_key?(key) | |
bucket.object(key).exists? | |
end | |
def bucket | |
@bucket ||= begin | |
s3 = Aws::S3::Resource.new | |
s3.bucket(@bucket_name) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment