Faster & efficient file upload in S3 using aws-sdk version 2.x
In my Rails(4.2.7) application, we were using aws-sdk ruby gem to interact with AWS S3.
While I was reading about how to upload large video files to S3 efficiently, I stumbled upon this question in stackoverflow : http://stackoverflow.com/questions/29105178/uploading-large-file-to-s3-with-ruby-fails-with-out-of-memory-error-how-to-read in stackoverflow.
We were using an older version of SDK (1.8.1.1), while the latest version is 2.6.6 as of writing this blog.
AWS-SDK version 2.x introduces a new API named Aws::S3::Resource which
- uses multipart APIs for large objects
- uses multiple threads to upload parts in parallel, improving upload speed
To access S3 in SDK 1.x we used to set AWS config as
However from version 2.x, region has to be mentioned otherwise it will complain about region as
“missing region; use :region option or export region name to ENV[‘AWS_REGION’] (Aws::Errors::MissingRegionError)”
To fix this just mention region in AWS config initializer as:
Another notable change is : I have mentioned logger config option as well for 2.x otherwise AWS-SDK will not log create, delete object loggers in the rails log file.
Creating a file in S3 :
Using AWS-SDK 1.x we used to upload a file to S3 like this :
where AWS_CONFIG
is Hash which contains environment specific AWS bucket name.
ENV['RAILS_ENV'] = "development" unless ENV['RAILS_ENV']
AWS_CONFIG = YAML.load(File.read("#{Rails.root}/config/aws.yml"))[ENV['RAILS_ENV']]
Using AWS-SDK 2.x, using the new Resource API, upload statement looks like this:
In the newer Resource API, the method to create a file in S3 bucket is put_object and it has various options that you can send along.
Refer http://docs.aws.amazon.com/sdkforruby/api/Aws/S3/Bucket.html#put_object-instance_method for detailed list of options.
Deleting a file in S3 :
Using AWS-SDK 1.x, command to delete a file is
Using AWS-SDK 2.x, it is