Skip to content

Instantly share code, notes, and snippets.

@bikerduweb
Created December 6, 2010 12:00
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bikerduweb/730194 to your computer and use it in GitHub Desktop.
Save bikerduweb/730194 to your computer and use it in GitHub Desktop.
Amazon + Reduced redundancy + Paperclip
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "IPDeny",
"Effect": "Deny",
"Principal": {
"AWS": "*"
},
"Action": "s3:*",
"Resource": "arn:aws:s3:::BUCKET_NAME/*",
"Condition": {
"NotIpAddress": {
"aws:SourceIp": [
"192.168.1.1/32",
"192.168.1.2/32",
"192.168.1.3/32"
]
}
}
}
]
}
server {
listen 80;
server_name tele1.zeworld.com tele2.zeworld.com tele3.zeworld.com tele4.zeworld.com;
set $asset_dir /var/www/replay.fr/current/public;
location @fetch {
access_log /var/log/nginx/amazon.log;
internal;
proxy_pass http://BUCKET_NAME.s3-external-3.amazonaws.com;
proxy_set_header Host BUCKET_NAME.s3-external-3.amazonaws.com;
proxy_store on;
proxy_store_access user:rw group:rw all:rw;
proxy_temp_path /tmp;
root $asset_dir/system;
}
location / {
root $asset_dir;
if (-f $request_filename) {
expires max;
break;
}
error_page 404 = @fetch;
}
require 'aws/s3'
require 'paperclip'
require 'paperclip/storage/s3'
# allow us to define Amazon storage type to use on Amazon for each style, by introducing a 's3_reduced' key
#
# Example:
#
# has_attached_file :image,
# :storage => :reduceds3,
# :s3_credentials => Rails.root.join("config/s3.yml"),
# :s3_permissions => 'public-read',
# :s3_protocol => 'http',
# :path => ":id.:style.:extension"
# :styles => {
# :original => {:geometry => "500x375>", :s3_reduced => false},
# :large => {:geometry => "300x250", :format => :png, :s3_reduced => true},
# :small => {:geometry => "100x75", :format => :png, :s3_reduced => true}
# },
# :processors => [:thumbnail],
# :default_style => :original
module Paperclip
module Storage
module Reduceds3
def self.extended base
base.extend(S3)
base.class_eval do
def flush_writes #:nodoc:
@queued_for_write.each do |style, file|
begin
# FIX OLIVIER : we personnalize headers
s3_headers = @s3_headers ? @s3_headers.dup : {}
if @options[:styles][style][:s3_reduced]
s3_headers['x-amz-storage-class'] = 'REDUCED_REDUNDANCY'
end
# puts "HEADERS: #{s3_headers.inspect}"
log("saving #{path(style)}")
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
}.merge(s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
retry
rescue AWS::S3::ResponseError => e
raise
end
end
@queued_for_write = {}
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment