Closed2

[Rails7] ActiveStorage&AmazonS3でオブジェクトを削除しようとすると「Aws::S3::Errors::AccessDenied」と怒られる

minorex15minorex15

2024/2/22時点の状況:

  • Renderでデプロイ中
  • ActiveStorageを使ってS3のバケットにアップロード済み
videos_controller.rb
class VideosController < ApplicationController
    # 中略
    def destroy
      video = ActiveStorage::Attachment.find(params[:id])
      if video.purge && video.destroy
        redirect_to video_path
      else
        render :edit
      end
    end
    
    private
    def video_params
      params.require(:video).permit(:title, :introduction, :video)
    end
end
storage.yml
# 中略
amazon:
  service: S3
  access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
  secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
  region: ap-northeast-1
  bucket: <バケット名>

Renderのログ


INFO -- : [a5829ab2-09e5-4433-bbfb-630c427f69fd] [Aws::S3::Client 403 0.429672 0 retries] delete_object() Aws::S3::Errors::AccessDenied Access Denied
INFO -- : [a5829ab2-09e5-4433-bbfb-630c427f69fd]   S3 Storage (430.5ms) Deleted file:
INFO -- : [a5829ab2-09e5-4433-bbfb-630c427f69fd] Completed 500 Internal Server Error in 452ms (ActiveRecord: 12.7ms | Allocations: 5542)
CRITICAL FATAL -- : [a5829ab2-09e5-4433-bbfb-630c427f69fd]   
INFO [a5829ab2-09e5-4433-bbfb-630c427f69fd] Aws::S3::Errors::AccessDenied (Access Denied):
INFO [a5829ab2-09e5-4433-bbfb-630c427f69fd] app/controllers/videos_controller.rb:29:in `destroy'

minorex15minorex15

ポリシーを確認

IAMユーザーのポリシーの許可を再度確認してみると余計なポリシーが含まれていたので削除して「S3 FullAccess」のみに変更する
👉解決した!

このスクラップは2ヶ月前にクローズされました