Using Rails 6 and CarrierWaveDirect, we're able to successfully POST a file to a presigned S3 URL, but then we encounter an error when trying to download the file from S3 to our server for processing.
Our model and uploader look like this:
# model
class FileAttachment < ApplicationRecord
mount_uploader :file, FileUploader
end
# uploader
class FileUploader < CarrierWave::Uploader::Base
include CarrierWaveDirect::Uploader
end
S3 responds to our direct uploads (POST) as expected with something like this:
{
"bucket" => "my-bucket",
"key" => "uploads/abcde-my-file.pdf",
"etag" => "12345"
}
Following the docs, we handle the response by saving the returned key attribute to a new model like so:
#file_attachment = FileAttachment.new file_key: params[:key]
#file_attachment.save
This seems to work fine, however, when it tries to download the file from S3 to our server for processing, that fails due to a 400 Bad Request:
irb(main)> #file_attachment.remote_file_url = #file_attachment.file.url
=> "https://my-bucket.s3-us-west-2.amazonaws.com/uploads/abcde-my-file.pdf?X-Amz-Expires=600&X-Amz-Date=20211009T202903Z&X-Amz-Algorithm=...&X-Amz-Credential=...&X-Amz-SignedHeaders=host&X-Amz-Signature=..."
irb(main)> #file_attachment.save
=> false
irb(main)> #file_attachment.errors.full_messages
=> ["File could not download file: 400 Bad Request"]
Despite the 400, I can confirm that #file_attachment.file.url is a valid, working URL, and when accessing the URL through a browser, the file is able to be downloaded just fine. For some reason though, it can't be downloaded from our app.
Can anyone spot what we're doing wrong? Or does anyone know of a better way to debug this ("400 Bad Request" isn't enough information). Thanks in advance!
Here's our bucket CORS:
[
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET",
"POST",
"PUT"
],
"AllowedOrigins": [
"*"
],
"ExposeHeaders": []
}
]
And our environment details:
ruby '2.7.2'
gem 'rails', '~> 6.0.3'
gem 'carrierwave', '~> 2.1.0'
gem 'carrierwave_direct', '~> 2.1.0'
Thanks in advance!
Bumping CarrierWave versions from 2.1.0 to 2.2.0 ended up fixing this. No other changes were necessary.
I'm having a hard time integrating Google Contacts into my Ruby (version 2.2.10) on Rails 5 (version 5.1.5) App.
When I execute the following code block, I receive "ArgumentError: unknown keyword: person_fields".
people = Google::Apis::PeopleV1::PeopleService.new
people.authorization = auth_client
response = people.list_person_connections('people/me', page_size: 10,
person_fields: 'names,emailAddresses')
To rectify the problem, I tried using the following gem versions:
gem 'google-api-client', '~> 0.11'
gem 'google-api-client', '~> 0.8'
gem 'google-api-client'
I still receive the error no matter which version of the gem I use.
Below is the code in its entirety:
require 'google/apis/people_v1'
require 'google/api_client/client_secrets'
client_secrets = Google::APIClient::ClientSecrets.load 'client_secret_1088824912015-f8asojku302s0mvcijgj7takse8pg8rg.apps.googleusercontent.com.json'
auth_client = client_secrets.to_authorization
auth_client.update!( :scope => 'https://www.googleapis.com/auth/contacts.readonly', :redirect_uri => 'http://localhost:3000/oauth2callback', :additional_parameters => { "access_type" => "offline", "include_granted_scopes" => "true" } )
auth_uri = auth_client.authorization_uri.to_s
# To exchange an authorization code for an access token, use the fetch_access_token! method:
auth_client.code = #auth_code#
auth_client.fetch_access_token!
people = Google::Apis::PeopleV1::PeopleService.new
people.authorization = auth_client
response = people.list_person_connections('people/me', page_size: 10, person_fields: 'names,emailAddresses')
Any assistance would be greatly appreciated.
Just to clarify for any future readers, Ray Baxte linked to docs corresponding to version 0.9.19 of the google-api-client gem - the current version is 0.27.1
I'm using 0.26.0 and the correct argument is person_fields - this obviously changed somewhere between 0.9.19 and 0.26.0
I follow the documentation in the generated API method definitions:
https://github.com/googleapis/google-api-ruby-client/blob/0.27.1/generated/google/apis/people_v1/service.rb#L591
The correct attribute is fields not person_fields. See docs here.
I am new to the softlayer rest APIs. We have a requirement where user will be allowed to add a additional SAN or Local Disk to the existing provisioned server in softlayer. For that I was referring to the REST API guide Our project is build on Ruby on Rails and we are using softlayer_api gem and so I was looking at the api ruby doc. But none of these links helped me. Are there any ruby examples for adding a disk ?
Please try the following example to upgrade a Virtual Guest in order to add a disk:
require 'rubygems'
require 'softlayer_api'
# Your SoftLayer API username.
SL_API_USERNAME = 'set me'
# Your SoftLayer API key.
SL_API_KEY = 'set me'
# Set the server id that you wish to upgrade.
server_id = 17850400
# Set the new item price id to upgrade the VSI
price_id = 51733 # 10 GB (SAN) "categoryCode": "guest_disk1", "name": "Second Disk"
# Order Template with all new item configurations
object_template = {'packageId'=> 0,
'prices'=> [
{
'id'=> price_id
}
],
'virtualGuests'=> [
{
'id'=> server_id
}
],
'properties'=> [
{
'name'=> 'NOTE_GENERAL',
'value'=> 'Adding a SAN disk'
},
{
'name'=> 'MAINTENANCE_WINDOW',
'value'=> 'now'
}
],
'complexType'=> 'SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade'
}
softlayer_client = SoftLayer::Client.new(:username => SL_API_USERNAME,
:api_key => SL_API_KEY)
product_order_service = softlayer_client.service_named('SoftLayer_Product_Order')
begin
result = product_order_service.verifyOrder(object_template)
puts 'Result: '
puts result.inspect
rescue Exception => e
puts 'Unable to add the new SAN Disk ...'
$stdout.print(e.inspect)
end
Note: Once your script is ready, please change from verifyOrder to placeOrder.
To get valid prices for upgrade, please review:
SoftLayer_Virtual_Guest::getUpgradeItemPrices
References:
SoftLayer_Product_Order
SoftLayer_Container_Product_Order_Virtual_Guest_Upgrade
upgrade_examples
I'm fairly new to Elixir and this is the first app that I'm attempting to release using exrm. My app interacts with a Redis database for consuming jobs from a queue (using exq), and also stores results of processed jobs in Redis using eredis.
My app works perfectly when I run it via iex -S mix, and it also runs great when compiled into an escript. However when I use exrm, the application compiles without any issue, but it crashes when I run it.
This is the crash output:
$ ./rel/my_app/bin/my_app console
{"Kernel pid terminated",application_controller,"{application_start_failure,my_app,{bad_return,{{'Elixir.MyApp',start,[normal,[]]},{'EXIT',{{badmatch,{error,{{'EXIT',{{badmatch,{error,{undef,[{eredis,start_link,[],[]},{'Elixir.MyApp.Cache',init,1,[{file,\"lib/my_app/cache.ex\"},{line,8}]},{gen_server,init_it,6,[{file,\"gen_server.erl\"},{line,306}]},{proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,237}]}]}}},[{'Elixir.MyApp.Cache',start_link,1,[{file,\"lib/my_app/cache.ex\"},{line,21}]},{supervisor,do_start_child,2,[{file,\"supervisor.erl\"},{line,314}]},{supervisor,handle_start_child,2,[{file,\"supervisor.erl\"},{line,685}]},{supervisor,handle_call,3,[{file,\"supervisor.erl\"},{line,394}]},{gen_server,try_handle_call,4,[{file,\"gen_server.erl\"},{line,607}]},{gen_server,handle_msg,5,[{file,\"gen_server.erl\"},{line,639}]},{proc_lib,init_p_do_apply,3,[{file,\"proc_lib.erl\"},{line,237}]}]}},{child,undefined,'Elixir.MyApp.Cache',{'Elixir.MyApp.Cache',start_link,[[{host,\"127.0.0.1\"},{port,6379},{database,0},{password,[]},{reconnect_timeout,100},{namespace,<<>>},{queues,[<<\"elixir\">>]}]]},permanent,5000,worker,['Elixir.MyApp.Cache']}}}},[{'Elixir.MyApp.Supervisor',start_cache,1,[{file,\"lib/my_app/supervisor.ex\"},{line,17}]},{'Elixir.MyApp.Supervisor',start_link,0,[{file,\"lib/my_app/supervisor.ex\"},{line,9}]},{'Elixir.MyApp',start,2,[{file,\"lib/my_app.ex\"},{line,10}]},{application_master,start_it_old,4,[{file,\"application_master.erl\"},{line,272}]}]}}}}}"}
Here is the mix.exs for my application:
defmodule MyApp.Mixfile do
use Mix.Project
def project do
[
app: :my_app,
version: "0.0.1",
name: "MyApp",
elixir: "~> 1.0",
escript: escript_config,
deps: deps
]
end
def application do
[
applications: app_list(Mix.env),
mod: { MyApp, [] },
env: [ queue: 'elixir']
]
end
def included_applications do
[ :logger, :httpoison, :eredis, :exq, :dotenv, :exjsx, :ex_doc, :oauth2, :sweet_xml ]
end
defp app_list(:dev), do: [:dotenv | app_list]
defp app_list(_), do: app_list
defp app_list, do: [:logger, :httpoison]
def escript_config do
[ main_module: MyApp ]
end
defp deps do
[
{ :dotenv, github: "avdi/dotenv_elixir" },
{ :eredis, github: "wooga/eredis", tag: "v1.0.5" },
{ :exjsx, "~> 3.1.0" },
{ :exq, "~> 0.1.0", app: false },
{ :exrm, "~> 0.16.0" },
{ :ex_doc, github: "elixir-lang/ex_doc" },
{ :httpoison, "~> 0.4" },
{ :oauth2, "~> 0.1.1" },
{ :sweet_xml, "~> 0.2.1" }
]
end
end
The crash appears to be happening in the following init function, where I call :eredis.start_link:
defmodule MyApp.Cache do
use GenServer
require Logger
def init(client_opts) do
{ :ok, client } = :eredis.start_link(
client_opts[:host],
client_opts[:port],
client_opts[:database],
client_opts[:password],
client_opts[:reconnect_timeout])
end
end
Could it be because eredis is an Erlang library as opposed to Elixir?
You need to add :eredis to your app_list function, so that it is packaged with the release, that goes for the rest of your dependencies as well.
I follow the link GitHub gem rich to install CKeditor to rails_admin
but I get the error: Unsupported field datatype: rich_editor
My model
edit do
field :title
field :description, :rich_editor do
config({
:insert_many => true
})
end
field :autho
field :book_type
end
How can I fix this error? Or that's an issue?
EDIT:
I tried it, and it worked
field :content, :text do
ckeditor do true end
end
I couldn't get the Rich gem to work with a Rails 4 project using Rails admin, so I decided to use the standard CK Editor Gem which is the recommended course of action by the authors. It took all of 5 minutes to get it working following this:
https://github.com/sferik/rails_admin/wiki/CKEditor
Then I configured my CK_Editor to use a small subset of the available functionality.
After adding the CK_Editor gem and configuring my rails admin initializer, I created a new javascript file in my project at:
/app/assets/javascripts/ckeditor/config.js
with the following contents:
CKEDITOR.config.toolbar = [
{ name: 'basicstyles', groups: [ 'basicstyles', 'cleanup' ],
items: [ 'Bold', 'Italic', 'Underline', 'Strike', '-', 'RemoveFormat' ] },
{ name: 'paragraph', groups: [ 'list', 'indent', 'blocks', 'align', 'bidi' ],
items: [ 'NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote',
'JustifyLeft', 'JustifyCenter', 'JustifyRight', 'JustifyBlock' ] },
{ name: 'links', items: [ 'Link', 'Unlink' ] },
];
Remember to restart your Rails server!
I have the same issue. I think it is an issue in rails_admin or in rich. I have successfully integrate these two together in past (but with old versions of both).
I have created github issues for this in rich (https://github.com/bastiaanterhorst/rich/issues/80) and rails_admin (https://github.com/sferik/rails_admin/issues/1585) repos.