Skip to content

Instantly share code, notes, and snippets.

@mabsboza
Created September 12, 2024 02:12
Show Gist options
  • Save mabsboza/48cbd46f1583de35f7b74726815dde69 to your computer and use it in GitHub Desktop.
Save mabsboza/48cbd46f1583de35f7b74726815dde69 to your computer and use it in GitHub Desktop.
require 'aws-sdk-s3'
require 'uri'
class S3Downloader
def initialize(region:, access_key_id:, secret_access_key:, bucket_name:)
@s3 = Aws::S3::Client.new(
region: region,
access_key_id: access_key_id,
secret_access_key: secret_access_key
)
@bucket_name = bucket_name
@temp_bucket_name = bucket_name
@output_path = @bucket_name.to_s # temporal location
end
def download_by_stencil_code(stencil_code, job_id)
stencil = DocStencil.find_by(code: stencil_code)
if stencil
doc_file = find_latest_approved_or_uploaded_doc_file(stencil.id, job_id)
if doc_file
download_file(doc_file)
else
puts "No se encontró ningún archivo relacionado con el stencil o la subida."
end
else
puts "No se encontró ningún stencil con el código: #{stencil_code}"
end
end
private
def find_latest_approved_or_uploaded_doc_file(stencil_id, job_id)
# Find the latest version that is approved or uploaded and not expired
DocFile.where(stencil_id: stencil_id)
.where(job_id: job_id)
.where('approved = ?', true)
.not_expired
.order(created_at: :desc)
.first
end
def download_file(doc_file)
key = extract_s3_key(doc_file.resource_url)
if key
file_path = File.join(@output_path, File.basename(key))
# Ensure the /tmp directory exists (it should be created automatically)
unless Dir.exist?(@output_path)
puts "Creating /tmp directory."
Dir.mkdir(@output_path)
end
# Download the file from the primary S3 bucket to /tmp
@s3.get_object(
bucket: @bucket_name,
key: key,
response_target: file_path
)
puts "Archivo descargado correctamente en #{file_path}"
# Upload the file to the temporary S3 bucket
download_url = upload_to_temp_s3(file_path)
puts "Archivo disponible para descargar en: #{download_url}"
else
puts "No se pudo extraer la clave del archivo desde la URL proporcionada."
end
rescue StandardError => e
puts "Ocurrió un error inesperado: #{e.message}"
end
def extract_s3_key(resource_url)
uri = URI.parse(resource_url)
# Busca el índice donde comienza "documents/" y extrae a partir de ahí
path_start_index = uri.path.index('documents/')
if path_start_index
# Extrae solo la parte desde "documents/" en adelante
path = uri.path[path_start_index..-1]
puts path
path
else
puts "No se encontró 'documents/' en la URL."
nil
end
end
def upload_to_temp_s3(file_path)
temp_key = File.basename(file_path)
# Upload the file to the temporary S3 bucket
@s3.put_object(
bucket: @temp_bucket_name,
key: "tmp/#{temp_key}",
body: File.open(file_path)
)
end
end
# Uso de la clase
downloader = S3Downloader.new(
access_key_id: ENV['AWS_ACCESS_KEY_ID'],
secret_access_key: ENV['AWS_SECRET_ACCESS_KEY'],
region: ENV['AWS_REGION'],
bucket_name: 'ksec-cencosud.ksec.cl'
)
# Reemplaza 'stencil_code' con el código del stencil que quieras buscar
stencil_code = 'OSS'
job_id = 1567
downloader.download_by_stencil_code(stencil_code, job_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment