Skip to content

Instantly share code, notes, and snippets.

@Bijendra
Last active May 30, 2023 08:20
Show Gist options
  • Star 29 You must be signed in to star a gist
  • Fork 13 You must be signed in to fork a gist
  • Save Bijendra/8861700 to your computer and use it in GitHub Desktop.
Save Bijendra/8861700 to your computer and use it in GitHub Desktop.
Upload images to AWS S3 in rails application using aws-sdk
Using gem aws-sdk for a ror application for uploading images to s3
Uploading images to a fixed bucket with different folders for each object or application.
The s3 keeps a limitation on the number of buckets creattion whereas there is no
limitation for content inside a bucket.
This code will upload image for a user to s3 using aws-sdk gem. The bucket and the image uploaded are made public
so that the images uploaded are directly accessible. The input it takes is the image complete path
where it is present, folder in which it should be uploaded and user_id for whom it should
be uploaded.
def save_screenshot_to_s3(image_location, folder_name,user_id)
service = AWS::S3.new(:access_key_id => ACCESS_KEY_ID,
:secret_access_key => SECRET_ACCESS_KEY)
bucket_name = "app-images"
if(service.buckets.include?(bucket_name))
bucket = service.buckets[bucket_name]
else
bucket = service.buckets.create(bucket_name)
end
bucket.acl = :public_read
key = folder_name.to_s + "/" + File.basename(image_location)
s3_file = service.buckets[bucket_name].objects[key].write(:file => image_location)
s3_file.acl = :public_read
user = User.where(id: user_id).first
user.image = s3_file.public_url.to_s
user.save
end
@syarynovskyi
Copy link

Thanks dude, you saved my ass! Really appreciated

@bmax329
Copy link

bmax329 commented Jan 8, 2015

this rocks. thanks.

@unavailabl3
Copy link

unavailabl3 commented Feb 8, 2019

Method for aws-sdk v3

  1. In bash (terminal) set ENV vars
    export AWS_ACCESS_KEY_ID='your_key_id'
    export AWS_SECRET_ACCESS_KEY='your_secret_key'

  2. code to upload
    require 'aws-sdk'
    s3 = Aws::S3::Resource.new(region: 'us-east-2') #check your region in s3 dashboard
    bucket = 'your_bucket_name'
    name = File.basename('/path/to/file.jpg')
    obj = s3.bucket(bucket).object(name)
    obj.upload_file(image_location)
    obj.public_url.to_s #returns public url of file at s3 server

If smth wrong check:
-region
-bucket_name and if it exists
-permissions in s3 dashboard
-CORS configuration, for example
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>http://localhost:3000</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

@fabellonnoe
Copy link

fabellonnoe commented Jun 12, 2019

  def save_image_to_s3
    image = "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAetc..."
    folder_name = 'downloadlinks'
    service = AWS::S3.new(:access_key_id => 'MyAccessKey',
                          :secret_access_key => 'MySecretKey')
    bucket_name = "MyBucketName"
    bucket = service.buckets[bucket_name]
    bucket.acl = :public_read
    key = folder_name.to_s + "/graph.png"
    s3_file = service.buckets[bucket_name].objects[key].write(:file => image)
    s3_file.acl = :public_read
  end 

it will return

<Errno::ENAMETOOLONG: File name too long @ rb_sysopen - data:image/jpeg;base64,/9

How can I upload base64 in the s3?

@Twofour2
Copy link

In case anyone else somehow winds up stuck here, as of version 3 of the ruby SDK you need to use Resource in order to directly access the bucket objects.
service = AWS::S3::Resource.new()

You can then access the bucket via service.bucket("bucket-name")

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Resource.html

@Bijendra
Copy link
Author

In case anyone else somehow winds up stuck here, as of version 3 of the ruby SDK you need to use Resource in order to directly access the bucket objects.
service = AWS::S3::Resource.new()

You can then access the bucket via service.bucket("bucket-name")

https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Resource.html

@chaos-a i'll check the new changes and will update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment