発生したエラー
下記のコードを実行すると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
で出力すると空文字が返ってきたことで気づくことができた。