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

问题描述 投票:1回答:1

我正在尝试使用下面的代码更新保存在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

谢谢

ruby-on-rails amazon-web-services amazon-s3 paperclip aws-sdk
1个回答
0
投票

您正在尝试将关键x的对象移动到关键nil。鉴于您的错误,我会首先尝试初始化一个新对象y然后调用move_to。

© www.soinside.com 2019 - 2024. All rights reserved.