-
-
Save Bijendra/8861700 to your computer and use it in GitHub Desktop.
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 |
Method for aws-sdk v3
-
In bash (terminal) set ENV vars
export AWS_ACCESS_KEY_ID='your_key_id'
export AWS_SECRET_ACCESS_KEY='your_secret_key'
-
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>
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?
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
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.
this rocks. thanks.