使用 aws-ruby-sdk 使用 AWS::S3 编写自定义文件名



有没有办法使用 aws-ruby-sdk gem 上传文件但使用动态(自定义)文件名? 这似乎是一个如此明显的需求,但在文档中却无处可寻。格勒

我在文档中看到的唯一方法是以下内容,它没有提供太多的灵活性。

## Uploading Files
## You can upload a file to S3 in a variety of ways. Given a path to a file (as a string) you can do any of the following:
# specify the data as a path to a file
obj.write(Pathname.new(path_to_file))
# also works this way
obj.write(:file => path_to_file)
# Also accepts an open file object
file = File.open(path_to_file, 'rb')
obj.write(file)
## All three examples above produce the same result. The file will be streamed to S3 in chunks. It will not be loaded entirely into memory.

到目前为止,我一直在使用 aws-s3 gem,它通过 AWS::S3::S3Object.store 支持此功能,但现在我正在使用 SDK gem,我无法同时使用它。

根据文档,初始化新对象的方式是

# new object, does not exist yet
obj = bucket.objects["my-text-object"]

然后,在实例上调用 .write 来写入文件。换句话说,相当于以下AWS::S3代码

S3Object.store('greeting.txt', 'hello world!', 'my-bucket')

应该是

obj = bucket.objects["greeting.txt"]
obj.write("hello world!")

其中bucket是表示存储桶的实例。

s3 = AWS::S3.new
bucket = s3.buckets['my-bucket']

最新更新