gpt4 book ai didi

ruby-on-rails - S3 文件复制的 AWS SDK v2 AllAccessDisabled 错误

转载 作者:行者123 更新时间:2023-12-04 04:50:04 25 4
gpt4 key购买 nike

我正在我编写的 Rails 应用程序中切换到新的 aws-sdk,但我一生都无法在 v2 sdk 中找到相应的方法。我也遇到了无法解决的访问被拒绝问题。

我使用 v1 sdk 的方式是用户使用“uploads”命名空间键直接上传到 s3,在他们创建他们正在处理的对象后,回调将文件移动到长期键并删除旧的.这是一个例子:

  def move_file
old_key = s3_key
new_key = "#{self.class.table_name}/#{id}/#{Digest::SHA1.hexdigest([Time.now, rand].join)}/#{filename}"
AWS.config(access_key_id: ENV['AWS_ACCESS_KEY_ID'], secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'], region: 'us-east-1')
s3 = AWS::S3.new
bucket_name = ENV['AWS_S3_BUCKET']
bucket = s3.buckets[bucket_name]
object = bucket.objects[old_key]

begin
object.move_to new_key, :acl => :public_read
rescue AWS::S3::Errors::NoSuchKey
errors.add(:base, "Oops! Something went wrong uploading your file. Please try again, and if the problem persists, open a trouble ticket.")
end

if !bucket.objects[old_key].exists? && bucket.objects[new_key].exists?
update_column(:s3_key, new_key)
end
end

效果很好,但现在我正在尝试更新到新的 sdk。我一直在尝试的是这样的:
  def move_file
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({copy_source:old_key, key:new_key, bucket: ENV['AWS_S3_BUCKET'], acl:'public-read'})
s3.delete_object({bucket: ENV['AWS_S3_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

每当我尝试移动上传的文件时,它都会抛出错误 - Aws::S3::Errors::AllAccessDisabled:已禁用对此对象的所有访问

我尝试改变处理安全凭证的方式。我没有使用裸访问 key / secret key 对,而是在 IAM 中创建了一个用户,附加了一个策略,授予他们对 S3 的完全访问权限,并尝试使用这些凭据,但无济于事。

我究竟做错了什么?而且,如果有人熟悉新的 sdk,我的 copy_object 方法是否正确?

最佳答案

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

"#{sourcebucket}/#{sourcekey}"

您的 old_key值包含一个正斜杠。 Amazon S3 使用该键的第一个路径段并将其视为存储桶名称。因为您没有该存储桶的权限,所以您会收到身份验证错误。您的凭据配置可能没问题。

要更正此错误:
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

关于ruby-on-rails - S3 文件复制的 AWS SDK v2 AllAccessDisabled 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28655936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com