0
1

More than 1 year has passed since last update.

s3とslackに同じファイルをアップロードするとエラーになる原因と対処法

Posted at

発生したエラー

下記のコードを実行するとSlack::Web::Api::Errors::ParsingError (parsing_error):になった。

sample_controller.rb
def send_file
    file = params[:file]
    # s3にアップロード
    s3 = Aws::S3::Client.new
    s3.put_object(
      bucket: AWS_S3_BUCKET,
      body: file,
      key: file_path,
    )
    # slackにもアップロード
    client = Slack::Web::Client.new
    client.files_upload(
        channels: channel_id,
        file: Faraday::UploadIO.new(file.tempfile, file.content_type),
        initial_comment: 'ファイルです'
      )
end

結論:ファイルの読み込み位置

s3.put_objectでファイルの読み込みが行われ、slack apiでfiles_upload実行時にはファイルの読み込み位置が終端になっているのが原因。

そのため、slackのアップロード処理の前にfile.tempfile.rewindで読み込み位置を先頭に戻すことで解決した。

p file.tempfile.readで出力すると空文字が返ってきたことで気づくことができた。

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