Last active
March 16, 2020 20:16
-
-
Save mikebuss/2daa8be801e7bcf84f74062927d9f663 to your computer and use it in GitHub Desktop.
Partial script for uploading to S3 in Ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "aws-sdk" | |
require_relative "errors" | |
class S3Uploader | |
AWS_ACCESS_KEY = ENV["AWS_ACCESS_KEY"] | |
AWS_SECRET_KEY = ENV["AWS_SECRET_KEY"] | |
AWS_BUCKET_NAME = ENV["AWS_BUCKET_NAME"] | |
AWS_REGION = ENV["AWS_REGION"] || "us-east-1" | |
AWS_FILE_PREFIX = ENV["AWS_FILE_PREFIX"] || "bm" | |
AWS_URL_CONTAINS_REGION = ENV["AWS_URL_CONTAINS_REGION"] || false | |
def self.upload(local_path, title, extension, file_type) | |
filename = "#{AWS_FILE_PREFIX}-#{SecureRandom.uuid}" | |
s3 = Aws::S3::Resource.new(region: AWS_REGION) | |
obj = s3.bucket(AWS_BUCKET_NAME).object("#{filename}.#{extension}") | |
if AWS_URL_CONTAINS_REGION | |
url = "https://s3.#{AWS_REGION}.amazonaws.com/#{AWS_BUCKET_NAME}/#{filename}.#{extension}" | |
else | |
url = "https://s3.amazonaws.com/#{AWS_BUCKET_NAME}/#{filename}.#{extension}" | |
end | |
obj.upload_file(local_path) | |
{ | |
url: url, | |
extension: File.extname(local_path).gsub(".", ""), | |
file_type: file_type, | |
filename: filename, | |
platform: "iOS", | |
title: title | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment