Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

put object to a specific folder in aws S3 ruby

I was trying to upload files using Aws sdk rails gem.

Everything is successful except I can not find a way how to upload files to a specific folder using ruby sdk.

s3 = Aws::S3::Resource.new
obj = s3.bucket('storagy-teen-dev-us').object("deepak_file")
obj.upload_file('./tmp/aws_test.txt')

This works fine. But I want to create the deepak_file inside a particular directory in aws bucket, let's say photos. I already tried the below but not working.

obj = s3.bucket('storagy-teen-dev-us').object("photos/deepak_file")

like image 754
Deepak Kumar Padhy Avatar asked Apr 18 '17 08:04

Deepak Kumar Padhy


2 Answers

Try this,

s3 = Aws::S3::Resource.new
path = 'photos/deepak_file/aws_test.txt'
s3.bucket('storagy-teen-dev-us').object(path).upload_file(./tmp/aws_test.txt)

you just need to specify full path of directory with file name.

like image 141
Hardik Upadhyay Avatar answered Sep 28 '22 00:09

Hardik Upadhyay


require 'aws-sdk-s3' # v1.x

client = Aws::S3::Client.new(region: '...',
                             access_key_id: '...',
                             secret_access_key: '...')

client.put_object(bucket: bucket,
                  key: 'photos/my-pic.jpg',
                  body: File.read(some_image_file),
                  acl: 'private')
like image 26
Kris Avatar answered Sep 28 '22 01:09

Kris