Skip to content

Instantly share code, notes, and snippets.

@amitsaxena
amitsaxena / get_s3_file_sizes_recursively.rb
Last active August 29, 2015 13:58
Get file size of all the files at a given path on Amazon S3 - recursively, and dump in a CSV
# Using aws-sdk gem
@access_key = "access_key"
@secret_key = "secret_access_key"
@bucket = "my_bucket"
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)
@amitsaxena
amitsaxena / gist:689d29f23d01bd563839
Last active August 29, 2015 14:07
Ruby text only spinner/loader/processing/progress indicators && some FUN
# Loader for infinite time
["|", "/", "-", "\\"].each{|v| STDOUT.write "\r#{v}"; sleep 0.5} while 1
# In case you want to run it for a few seconds (~ 20s in the example below)
10.times{ ["|", "/", "-", "\\"].each{|v| STDOUT.write "\r#{v}"; sleep 0.5} }
# Progress Bar
100.times{STDOUT.write "|"; sleep 0.1}
# Another kind of loading indicator
def url_for_oauth_code(options = {})
# for permissions, see http://developers.facebook.com/docs/authentication/permissions
permissions = options[:permissions]
scope = permissions ? "&scope=#{permissions.is_a?(Array) ? permissions.join(",") : permissions}" : ""
display = options.has_key?(:display) ? "&display=#{options[:display]}" : ""
callback = options[:callback] || @oauth_callback_url
raise ArgumentError, "url_for_oauth_code must get a callback either from the OAuth object or in the options!" unless callback
class Link < ActiveRecord::Base
validate :url_presence
def url_presence
errors.add(:base, "#{Link.url_label(linktype)} can't be blank.") if url.blank?
end
def self.url_label(linktype)
{"web" => "Web URL", "video" => "Video URL", "audio" => "Audio URL", "ecommerce" => "Shop URL",
"page" => "Page number", "email" => "Email"}[linktype] || "URL"
Ruby aws-sdk has poor documentation, and I wasn't able to locate it as well. Below is a function that I have created to check whether a file has read permission or not. Modify it as per your needs:
def check_if_public_read(object)
object.acl.grants.each do |grant|
begin
if(grant.grantee.uri == "http://acs.amazonaws.com/groups/global/AllUsers")
return true if ([:read, :full_control].include?(grant.permission.name))
end
rescue
end
@amitsaxena
amitsaxena / gist:5209033
Last active December 15, 2015 05:29
Get a recursive listing of all files at at a given Amazon S3 path
credentials = YAML.load_file("#{Rails.root}/config/s3.yml")
@access_key = credentials[Rails.env]["access_key_id"]
@secret_key = credentials[Rails.env]["secret_access_key"]
@bucket = credentials[Rails.env]["bucket"]
AWS.config(
:access_key_id => @access_key,
:secret_access_key => @secret_key
)
@s3 = AWS::S3.new
@amitsaxena
amitsaxena / gist:8625016
Created January 25, 2014 22:59
Custom URL: Launch app if app is installed, else open an alternate URL (iOS Safari only)
<script type="text/javascript">
var timer;
window.addEventListener("pageshow", function(evt){
clearTimeout(timer);
}, false);
window.addEventListener("pagehide", function(evt){
clearTimeout(timer);
}, false);
@amitsaxena
amitsaxena / s3_bucket_policy.json
Last active June 20, 2016 22:19
How to return a 404 response code for CloudFront requests (S3 origin) of a non-existent S3 object: http://aawaara.com/post/146226425447/how-to-return-a-404-response-code-for-cloudfront
{
"Version": "2008-10-17",
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "Enter your CloudFront Origin Access Identity here"
},
@amitsaxena
amitsaxena / gist:6977506
Last active February 7, 2017 18:33
How to get object creation timestamp from BSON object id using mongoid, and do range queries on it.
# To get the created at timestamp
sample_object.id.generation_time
# To do range queries
start = Moped::BSON::ObjectId.from_time(Time.now.beginning_of_day)
finish = Moped::BSON::ObjectId.from_time(Time.now.end_of_day)
MyModel.where(:id => {'$gt' => start, '$lt' => finish}).count
@amitsaxena
amitsaxena / migrations.js
Last active September 2, 2017 12:05
Quickstart guide: Migrations with node.js
// Install required modules as below:
npm install -g db-migrate
npm install -g db-migrate-mysql
// Create a database.json file:
{
"development": {
"driver": "mysql",
"host": "localhost",