简体   繁体   English

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

[英]How to Rename a file on S3 bucket after save using 'aws-sdk'?

I'm trying to update a file name saved on aws s3 using below code 我正在尝试使用以下代码更新保存在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)

when updating giving an error on line obj.move_to("#{bucket.name}/#{new_file}", acl: 'public-read') : 更新时,在obj.move_to("#{bucket.name}/#{new_file}", acl: 'public-read')行上给出错误:

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

Any suggestions or ideas how to rename a file on s3? 有什么建议或想法如何重命名s3上的文件?

object.inspect output: object.inspect输出: 在此处输入图片说明

Thanks 谢谢

You are trying to move an object with key x to key nil . 您试图将键x的对象移动到键nil Given your error I would try initializing a new object y first then call the move_to on it. 鉴于您的错误,我将尝试首先初始化一个新对象y然后在其上调用move_to。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM