Direct upload files to S3 - Ruby Code
In this blog, we will see how one can directly upload file to S3 bucket.
Firstly, you’ll need an AWS account and remember that S3 (Simple Storage Service) is not free service. You need to pay as per your usage.
Following are the charges dated on 28/01/2023:
Now let’s start step by step:
- First, you need to sign in to your AWS account and search for S3 and click on create bucket.
- After clicking on create bucket, you need to enter bucket name as per your use. Here i’ll be entering
demofordirectupload
as bucket name as follows:
Note that bucket name should be globally unique and must not contain spaces or uppercase letters.
Click on create bucket as we are not changing any default configuration.
- Now that we have successfully created S3 bucket, let’s move to ruby code which will directly upload file to created bucket.
- In order to upload file from ruby code, we’ll need following info
- AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID
- bucket name
- AWS region
-
In order to generate AWS secret keys, I’ve created new user and generated access keys inside IAM Security Credentials.
Important note. You need to give permission to upload s3 file from permissions in IAM dashboard. You can create new policy and attach that policy to user in order to grant permission.
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
require 'aws-sdk-s3'
AWS_ACCESS_KEY_ID = "SOMERANDOMGENERATEDSTRING"
AWS_SECRET_ACCESS_KEY = "MORELENGTHYRAMDOMEGENERATEDSTRING"
REGION = "us-east-2"
BUCKET = "bucketname"
def get_s3_client
Aws::S3::Client.new(
access_key_id: AWS_ACCESS_KEY_ID,
secret_access_key: AWS_SECRET_ACCESS_KEY,
region: REGION,
)
end
def direct_upload
s3 = get_s3_client
path = "FILE_PATH"
File.open(path, 'rb') do |file|
p 'Started uploading file to S3'
response = s3.put_object(
bucket: BUCKET,
key: path,
body: file,
acl: 'bucket-owner-full-control'
)
p response
end
puts "File should be available at https://#{BUCKET}.s3.#{REGION}.amazonaws.com/#{path}"
end
direct_upload
In above code, please replace AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
with your keys and same goes for bucket.
After you’ve replaced these things, you’ll see output as:
We can see uploaded file as above 👆🏼. Cheers 🥳