Use ruby to upload photos to google cloud bucket
require "google/cloud/storage" | |
def procdir(dir) | |
Dir[ File.join(dir, '**', '*') ].reject { |p| File.directory? p } | |
end | |
def same_file(f, f2) | |
return File.exist?(f) && File.exist?(f2) && (File.size(f) == File.size(f2)) | |
end | |
def get_file_name(f) | |
return f.split("/").pop().to_s | |
end | |
def get_new_name(f, dest) | |
name = get_file_name(f) | |
attempted = dest+name | |
divided = attempted.split(".") | |
extension = divided.pop() | |
modified = '' | |
n = 1 | |
loop do | |
modified = divided.join(".")+"-"+n.to_s+"."+extension | |
n += 1 | |
break if !File.exist?(modified) | |
end | |
return modified | |
end | |
project_id = "INSERT-PROJECT-ID" | |
storage = Google::Cloud::Storage.new( | |
project: project_id, | |
keyfile: "/INSET-KEY-PATH.json" | |
) | |
bucket = storage.bucket "INSERT-BUCKET-NAME" | |
i = j = k = l = m = 0 | |
procdir("/data/").each { |f| | |
remote_file_path = "Pictures/" + f.split("/").pop().to_s | |
remote_file = bucket.file remote_file_path | |
if remote_file == nil | |
local_copy = "/data2/"+f.split("/").pop().to_s | |
if File.exist?(local_copy) | |
if same_file(f, local_copy) | |
puts f + " = ignored" | |
j += 1 | |
else | |
new_name = get_new_name(f, "/data2/") | |
FileUtils.cp f, get_new_name(f, "/data2/") | |
puts new_name + " = renamed" | |
k += 1 | |
end | |
else | |
puts f + " = perfect" | |
FileUtils.cp f, local_copy | |
m += 1 | |
end | |
i += 1 | |
else | |
puts f + " = already" | |
l += 1 | |
end | |
} | |
puts "Count "+i.to_s | |
puts "Already "+l.to_s | |
puts "Ignored "+j.to_s | |
puts "Renamed "+k.to_s | |
puts "Perfect "+m.to_s | |
puts "------" | |
procdir("/data2/").each { |f| | |
remote_file_path = "Pictures/" + f.split("/").pop().to_s | |
remote_file = bucket.file remote_file_path | |
if remote_file == nil | |
puts "Uploading "+remote_file_path | |
bucket.create_file f, remote_file_path | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment