且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

适用于S3文件复制的AWS开发工具包v2 AllAccessDisabled错误

更新时间:2022-11-07 14:57:06

该错误是由传递给#copy_object:copy_source值引起的.此值必须是源存储桶和源密钥,并用斜杠(/)分隔:

The error is caused by the :copy_source value you are passing to #copy_object. This value must be the source bucket and source key, separated by a slash (/):

"#{sourcebucket}/#{sourcekey}"

您的old_key值包含一个正斜杠. Amazon S3正在采用该密钥的第一个路径段,并将其视为存储桶名称.由于您没有该存储桶的权限,因此收到了auth错误.您的凭据配置可能还不错.

Your old_key value contains a forward slash. Amazon S3 is taking the first path segment of that key and treating it as a bucket name. Because you do not have permission to that bucket, you are getting an auth error. Your credential configuration is probably just fine.

要更正此错误:

def move_file
  bucket = ENV["AWS_S3_BUCKET"]
  old_key = file
  new_key = "#{self.class.table_name}/#{id}/#{Digest::SHA1.hexdigest([Time.now, rand].join)}/#{filename}"
  s3 = Aws::S3::Client.new

  begin
    s3.copy_object(bucket:bucket, key:new_key, copy_source:"#{bucket}/#{old_key}", acl:'public-read')
    s3.delete_object(bucket:bucket, key:old_key)
    update_column(:file, new_key)
  rescue Aws::S3::Errors::ServiceError
    errors.add(:base, "Oops! Something went wrong uploading your file. Please try again, and if the problem persists, open a trouble ticket.")
  end
end