0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【Ruby】S3にCSVデータを出力する

Posted at

はじめに

  • 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
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?