일부 데이터 및 base64 이미지로 내 컨트롤러에 아약스 거부를 만들고 있는데이 이미지를 s3에 업로드하고 base64를 이미지 URL로 바꾸기를 원합니다. 나는 지금이 https://sebastiandobrincu.com/blog/how-to-upload-images-to-rails-api-using-s3rails 컨트롤러에서 base64 이미지를 s3에 업로드하는 방법은 무엇입니까?
def split_base64(uri_str)
if uri_str.match(%r{^data:(.*?);(.*?),(.*)$})
uri = Hash.new
uri[:type] = $1 # "image/gif"
uri[:encoder] = $2 # "base64"
uri[:data] = $3 # data string
uri[:extension] = $1.split('/')[1] # "gif"
return uri
else
return nil
end
end
def convert_data_uri_to_upload(image_url)
image_data = split_base64(image_url)
image_data_string = image_data[:data]
image_data_binary = Base64.decode64(image_data_string)
temp_img_file = Tempfile.new("")
temp_img_file.binmode
temp_img_file << image_data_binary
temp_img_file.rewind
img_params = {:filename => "image.#{image_data[:extension]}", :type => image_data[:type], :tempfile => temp_img_file}
uploaded_file = ActionDispatch::Http::UploadedFile.new(img_params)
end
return uploaded_file
end
따라하고 내 컨트롤러에서 나는 convert_data_uri_to_upload(base64_image)
가 지금은 AWS 자격 증명을 쓰기 위치를 모르는 통과하고있다. URL에 따르면 Fog.rb 파일에 자격 증명을 써야하지만 그런 파일은 없습니다. 나는 CarrierWave를 확장하고 구성을 작성한 업 로더 폴더 안에 하나의 ImageUploader를 만들었지 만 작동하지 않습니다.
를, [것이 바람직 할 것이다 (// meta.stackoverflow.com/q/8259가)의 핵심 부품을 포함하는 대답은 여기에 있으며 참조 용 링크를 제공합니다. – kayess