【问题标题】:Write custom file name with AWS::S3 using aws-ruby-sdk使用 aws-ruby-sdk 使用 AWS::S3 编写自定义文件名
【发布时间】:2014-01-07 03:06:31
【问题描述】:

有没有办法使用aws-ruby-sdk gem 上传文件但具有动态(自定义)文件名?这似乎是一个显而易见的需求,但在documentation 中却找不到它。呜呜呜

我在文档中看到的唯一方法如下,它没有提供太大的灵活性。

## 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,我不能同时使用它。

【问题讨论】:

    标签: ruby-on-rails ruby amazon-web-services amazon-s3


    【解决方案1】:

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

    # 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']
    

    【讨论】:

    • 我明白了,所以“key”或“my-text-object”是自定义文件名。谢谢
    猜你喜欢
    • 2020-09-01
    • 2018-08-02
    • 1970-01-01
    • 2017-04-13
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    相关资源
    最近更新 更多