Uploading an image to S3 using aws-sdk v2 - ruby

I'm having a hell of a time working with the aws-sdk documentation, all of the links I follow seem outdated and unusable.
I'm looking for a straight forward implementation example of uploading an image file to an S3 bucket in Ruby.
let's say the image path is screenshots/image.png
and I want to upload it to the bucket my_bucket
AWS creds live in my ENV
Any advice is much appreciated.

Here is how you can upload a file from disk to the named bucket and key:
s3 = Aws::S3::Resource.new
s3.bucket('my_bucket').object('key').upload_file('screenshots/image.png')
That is the simplest method. You should replace 'key' with the key you want it stored with in Amazon S3. This will automatically upload large files for you using the multipart upload APIs and will retry failed parts.
If you prefer to upload always using PUT object, you can call #put or use an Aws::S3::Client:
# using put
s3 = Aws::S3::Resource.new
File.open('screenshots/image.png', 'rb') do |file|
s3.bucket('my_bucket').object('key').put(body:file)
end
# using a client
s3 = Aws::S3::Client.new
File.open('screenshots/image.png', 'rb') do |file|
s3.put_object(bucket:'my_bucket', key:'key', body:file)
end
Also, the API reference documentation for the v2 SDK is here: http://docs.aws.amazon.com/sdkforruby/api/index.html

Related

How to write to get content from Google Cloud Storage in Ruby?

Is it possible to use the Google Cloud Function to retrieve the contents of a file placed in Google Cloud Storage?
Also, I would like to know how to do this.
I tried this,
file = bucket.file(path)
file.download.read
but I didn't read response
\xA3\xB4\x97..
file = bucket.file(path)
downloaded_file = file.download
File.read(downloaded_file, encoding: 'ENCODE') # utf8 etc.
Also read this answer.
GL.

Laravel get image from S3 instead of local

I have a Laravel 5 app which was previously storing images locally. I have now modified it so that images are stored on an S3 server, I previously retrieved the local images like this...
$image_contents = Storage::get('myimages/logos/' . $image->filename);
Now I have moved to S3 storage, how can I instead get the image from the S3 bucket?
It's the same but you have to put your AWS bucket path:
Storage::disk('s3')->get('AWS_BUCKET_PATH');

upload image bytes to cloudinary upload API

I am using cloudinary API to get different resolution of the image/video files.I am able to use upload API using following code and get the required resolutions from java code
Map uploadResult = cloudinary.uploader().upload(file, options);
Now i need to perform the same from aws lamda using java code since I need to get resolutions of content stored in s3 bucket. I can think of 2 possible ways of doing the same 1) point cloudinary to use s3 urls - this requires setup 2)byte array buffer or IO input stream. Is there any sample java code available to option 2 ?
I am referring to upload API here
https://cloudinary.com/documentation/image_upload_api_reference#upload
This has some reference with python
Correct way for uploading image bytes to cloudinary
Please check this example:
File file = new File("<image_path>");
byte[] fileContent = Files.readAllBytes(file.toPath());
cloudinary.uploader().upload(fileContent, ObjectUtils.emptyMap()));
--Yakir

Write to a AWS S3 pre-signed url using Ruby

I want to upload a file to S3 using a pre-signed url
First I create a signed url using url_for(:write)
s3object = S3.buckets['myBucket'].objects['someFolder/testFile.txt']
url = s3object.url_for(:write) #
And I get the url as expected.
https://s3.amazonaws.com/myBucket/someFolder/testFile.txt?AWSAccessKeyId=AKJFGKJASGK......
But now I want to upload something using this url
I's assume that there is a way to get an s3 object from that url on which I can use the 'write' method.
Something like obj = getObjectFromUrl(url)
Is there such a method?
If not, how should I use this url to upload something to s3?
EDIT: I probably should explain.
I create the URL for testing sake.
When I will be working on it, I will only have the URL.
Using the URL alone, I will have to upload a file.
Via the current AWS SDK documentation for Ruby, the suggested method is to simply call read on your s3 object. If you already have the s3 object, you should be able to read directly from the object itself. Also, there is a method for streaming downloads if you wish to stream a file read. AWS S3 Object Documentation
obj.write('abc')
puts obj.read
#=> abc

How to upload file and save it directly to MongoDB using GridFS

I have a Sinatra application hosted on heroku and I'm trying to enable file uploading. I know heroku doesn't allow saving to the file system so I'm trying to save the image to MongoDB using GridFS directly. But I don’t know how.
Using the code below, I'm able to save to file system
base_dir = Dir.pwd + "/static/images/channels/"
File.open("#{base_dir}" + params['logo'][:filename], "w") do |f|
f.write(params['logo'][:tempfile].read)
end
How do I save the file directly to MongoDB without first saving it to the file system?
You can use the GridFS API to basically do what you're doing above, but write to MongoDB: http://api.mongodb.org/ruby/current/Mongo/GridFileSystem.html#open-instance_method.
I think you need to upload the file as binary data to a database.
You can use PaperClip to upload files and then store them as binary to MangoDB.
here this link might help you out:
If your files are actually less than 16 mb, please try using this Converter that changes the image of format jpeg / png to a format of saving to mongodb, and you can see this as an easy alternative for gridfs ,
please follow this github repo for more details
https://github.com/saran-surya/Mongo-Image-Converter

Resources