如何使用' aws-sdk'?进行保存后,在S3存储桶上重命名文件?

时间:2017-12-20 14:00:30

标签: ruby-on-rails amazon-web-services amazon-s3 paperclip aws-sdk

我尝试使用以下代码更新保存在aws s3上的文件名

gem 'aws-sdk', '~> 2.3'

creds = Aws::Credentials.new(ENV['AWS_ACCESS_KEY_ID'], ENV['AWS_SECRET_ACCESS_KEY'])

s3 = Aws::S3::Resource.new(credentials: creds, region: ENV['AWS_REGION'])

bucket = s3.bucket(ENV['AWS_S3_BUCKET'])

filename = URI.unescape(document.url).split('/').last

new_file = 'new_file_name'

obj = bucket.object(filename) # create object of original file name

obj.move_to("#{bucket.name}/#{new_file}", acl: 'public-read')

obj = bucket.object(new_file)

更新时在行obj.move_to("#{bucket.name}/#{new_file}", acl: 'public-read')上发出错误:

Aws::S3::Errors::NoSuchKey 
The specified key does not exist.

如何在s3上重命名文件的任何建议或想法?

object.inspect输出: enter image description here

由于

1 个答案:

答案 0 :(得分:0)

您正尝试将包含密钥x的对象移至密钥nil。鉴于您的错误,我会先尝试初始化一个新对象y然后再调用move_to。