yekta
yekta

Reputation: 3433

Write custom file name with AWS::S3 using aws-ruby-sdk

Is there a way to upload a file but with a dynamic (custom) filename using the aws-ruby-sdk gem? This seems like such an obvious need, yet its nowhere to be found in the documentation. Grrr

The only method I see in the docs are the following, which doesn't offer much flexibilty.

## 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.

I've been using the aws-s3 gem until now which supported this with the AWS::S3::S3Object.store, but now that I'm using the SDK gem I can't use this concurrently.

Upvotes: 1

Views: 1721

Answers (1)

Simone Carletti
Simone Carletti

Reputation: 176402

According to the documentation, the way you initialize a new object is

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

then you invoke .write on the instance to write the file. In other words, the equivalent of the following AWS::S3 code

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

should be

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

where bucket is an instance representing a bucket.

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

Upvotes: 2

Related Questions