54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
# File 'lib/capistrano/s3-mirror.rb', line 54
def sync (key_id, secret_access_key, bucket_name, base_path, s3_region_endpoint)
AWS::S3::DEFAULT_HOST.replace(s3_region_endpoint)
AWS::S3::Base.establish_connection!(:access_key_id => key_id, :secret_access_key => secret_access_key)
bucket = AWS::S3::Bucket.find(bucket_name)
context = File.join(bucket_name, base_path).split("/")
remote_data = bucket.objects.select{ |object| object.content_type != "application/x-directory"}
remote_data = remote_map(remote_data, context)
remote = Hash[remote_data]
local_data = Dir.chdir("public") do
Dir["**/**"].select{ |d| !File.directory? d }.map { |path| [ path, Digest::MD5.file(path).hexdigest ] }
end
local = Hash[local_data]
new = local.keys - remote.keys
deleted = remote.keys - local.keys
both = local.keys & remote.keys
uploaded_count = 0
deleted_count = 0
updated_count = 0
Dir.chdir("public") do
new.each do |entry|
AWS::S3::S3Object.store(entry, open(entry), bucket_name, :access => :public_read)
uploaded_count += 1
end
deleted.each do |entry|
object = AWS::S3::S3Object.find(entry, bucket_name)
object.delete
deleted_count += 1
end
both.each do |entry|
if local[entry] != remote[entry].etag
AWS::S3::S3Object.store(entry, open(entry), bucket_name)
updated_count += 1
end
end
end
puts "uploaded #{uploaded_count}, deleted #{deleted_count}, updated #{updated_count}"
end
|