Upload a file in S3 without any form

Uploading a file in S3 is a normal scenario but all I got is uploading the file using a form but what if I want it to upload it without any form.

There are very simple steps to upload a file or object in S3 without any form.

def s3_resource @s3 = Aws::S3::Resource.new(region: ENV['aws_region'])
end
def s3_bucket s3_resource
@s3.bucket(ENV['aws_bucket'])
end
def upload_file_to_s3(file)
s3_resource
file_name = File.basename(file)
obj = @s3.bucket(ENV['aws_bucket']).object('certificates/' + file_name)
obj.upload_file(file)
end

As you can see these methods

The first method s3_resource  is just creating a instance of your S3.

The second method s3_bucket is initializing your bucket.

Now the third one upload_file_to_s3(file) basically uploads your file in S3.

In the third method

In the first line initiating the s3_resource.

In the second line fetching the file_name which can be abc.txt because we are fetching the basename of a file.

In the third line creating the object in a bucket for a place where we have to put our object.

Now in the last line uploading the file to S3.

Done!

Comments