はじめに
- CSVファイルのデータをAWSのS3に保存するロジックを実装したので、よかったら参考にしてみてください。
Rails: 5.2.6
Ruby: 2.7.2
実装したコード
- S3のバケットに 例:
2021_10/2021_10_01.csv
といったファイル名で出力されます。 - こちらは毎日バッチで実行することを想定して作成おります。
export_csv.rb
# frozen_string_literal: true
require 'csv'
require 'aws-sdk-s3'
class ExportCsv
def self.execute(params)
output = CSV.generate do |csv|
csv << params['headers']
params['values'].each do |value|
csv << value
end
end
export_csv_to_s3(output, params)
end
def self.export_csv_to_s3(data, params)
s3_client = Aws::S3::Client.new(stub_responses: Rails.env.test?,
access_key_id: params['access_key_id'],
secret_access_key: params['secret_access_key'],
region: params['region'])
bucket_name = params['bucket_name']
folder_name = Time.current.yesterday.strftime('%Y_%m').to_s
file_name = "#{Time.current.yesterday.strftime('%Y_%m_%d')}.csv"
file_full_path = "#{folder_name}/#{file_name}"
# バケットの存在確認とアクセス権の確認
if bucket_exists_and_accessible?(s3_client, bucket_name)
Rails.logger.info "Bucket '#{bucket_name}' exists and is accessible to you."
else
Rails.logger.info "Bucket '#{bucket_name}' does not exist " \
'or is not accessible to you.'
end
# ファイルアップロードが完了したか確認
if file_uploaded?(s3_client, bucket_name, file_full_path, data)
Rails.logger.info "Object '#{file_full_path}' uploaded to bucket '#{bucket_name}'."
else
Rails.logger.info "Object '#{file_full_path}' not uploaded to bucket '#{bucket_name}'."
end
end
def self.bucket_exists_and_accessible?(s3_client, bucket_name)
s3_client.head_bucket(bucket: bucket_name)
return true
rescue StandardError
return false
end
def self.file_uploaded?(s3_client, bucket_name, path, data)
response = s3_client.put_object(
bucket: bucket_name,
key: path,
body: data,
content_type: 'text/csv'
)
return true if response.etag
rescue StandardError => e
Rails.logger.info "Error uploading object: #{e.message}"
return false
end
private_class_method :export_csv_to_s3,
:bucket_exists_and_accessible?,
:file_uploaded?
end