嘿,我在让Arc上传图片到我的亚马逊S3帐户时遇到了问题。我不知道问题是配置应用程序是否正确连接到帐户或如何正确设置上传文件。我尝试了几种做事方式但没有取得成功。
首先,我尝试在secret.config.exs
文件中设置我的密钥ID和密钥访问密钥,如下所示:
config :ex_aws,
access_key_id: "My key ID was here",
secret_access_key: "My secret access key was here",
region: "us-west-2",
s3: [
scheme: "https://",
host: "s3.amazonaws.com",
region: "us-west-2"
]
但没有成功。我再次尝试使用系统环境变量在配置文件中设置它们,如下所示:
config :arc,
bucket: "callmemd"
config :ex_aws,
access_key_id: [{:system, "AWS_ACCESS_KEY_ID"}, :instance_role],
secret_access_key: [{:system, "AWS_SECRET_ACCESS_KEY"}, :instance_role]
但是,我不确定我是否正确地做到了。这是我试图用来调用上传的控制器中的代码,上传的文件正在名为avatar的param中正确保存为上传插件:
def create(conn, %{"doctor" => doctor_params}) do
avatar = doctor_params["avatar"]
Myapp.Avatar.store({avatar.filename, avatar.path})
我还尝试了以下不同的配置和许多其他变种而没有运气:
Callme.Avatar.store({doctor_params["avatar"], doctor_params["id"]})
Callme.Avatar.store({%Plug.Upload{doctor_params}, doctor_params["id"]})
Callme.Avatar.store({%Plug.Upload{}, doctor_params["id"]})
Callme.Avatar.store({doctor_params["avatar"], doctor_params["id"]})
以下是我的依赖
defp deps do
[{:phoenix, "~> 1.1.4"},
{:postgrex, ">= 0.0.0"},
{:phoenix_ecto, "~> 2.0"},
{:phoenix_html, "~> 2.4"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.9"},
{:comeonin, "~> 1.0"},
{:mailgun, "~> 0.1.2"},
{:poison, "~> 2.1", override: true},
{:cowboy, "~> 1.0"},
{:arc, "~> 0.5.2"},
{:ex_aws, "~> 0.4.10"},
{:httpoison, "~> 0.7"}]
end
混合文件:
def application do
[mod: {Callme, []},
applications: [:phoenix, :phoenix_html, :cowboy, :ex_aws, :httpoison, :logger, :gettext, :phoenix_ecto, :postgrex]]
end
成功调用Create动作时的日志:
[info] POST /doctors
[debug] SELECT u0."id", u0."name", u0."email", u0."crypted_password", u0."admin", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [26] OK query=0.0ms
[debug] Processing by Callme.DoctorController.create/2
Parameters: %{"_csrf_token" => "XxEtCRshOjg5Wj1ZLwIzWmYzPwYBJgAAmtGqxfcHa3q1mOF8WfGDLQ==", "_utf8" => "✓", "doctor" => %{"avatar" => %Plug.Upload{content_type: "image/jpeg", filename: "beta.jpg", path: "C:\\Users\\Frank\\AppData\\Local\\Temp/plug-1469/multipart-622422-206000-2"}, "bio" => "bio", "hiddeninfo" => "hiddeninfo", "name" => "name", "picture" => "picture", "specialty" => "specialty"}}
Pipelines: [:browser]
[debug] BEGIN [] OK query=0.0ms
[debug] INSERT INTO "doctors" ("inserted_at", "updated_at", "bio", "hiddeninfo", "name", "picture", "specialty") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id" [{{2016, 7, 27}, {12, 27, 2, 0}}, {{2016, 7, 27}, {12, 27, 2, 0}}, "bio", "hiddeninfo", "name", "picture", "specialty"] OK query=0.0ms
[debug] COMMIT [] OK query=16.0ms
[info] Sent 302 in 32ms
[info] GET /doctors
[debug] SELECT u0."id", u0."name", u0."email", u0."crypted_password", u0."admin", u0."inserted_at", u0."updated_at" FROM "users" AS u0 WHERE (u0."id" = $1) [26] OK query=0.0ms
[debug] Processing by Callme.DoctorController.index/2
Parameters: %{}
Pipelines: [:browser]
[debug] SELECT d0."id", d0."name", d0."hiddeninfo", d0."bio", d0."specialty", d0."picture", d0."inserted_at", d0."updated_at" FROM "doctors" AS d0 [] OK query=0.0ms
我稍微调整了一下代码,现在我得到的错误如下
Request: POST /doctors
Jul 28 10:28:37 callmemd app/web.1: ** (exit) an exception was raised:
Jul 28 10:28:37 callmemd app/web.1: ** (KeyError) key :id not found in: %Plug.Upload{content_type: "image/jpeg", filename: "file000166349580.jpg", path: "/tmp/plug-1469/multipart-726916-266069-2"}
Jul 28 10:28:37 callmemd app/web.1: (callme) web/controllers/doctor_controller.ex:21: Callme.DoctorController.create/2
Jul 28 10:28:37 callmemd app/web.1: (callme) web/controllers/doctor_controller.ex:1: Callme.DoctorController.action/2
Jul 28 10:28:37 callmemd app/web.1: (callme) web/controllers/doctor_controller.ex:1: Callme.DoctorController.phoenix_controller_pipeline/2
Jul 28 10:28:37 callmemd app/web.1: (callme) lib/phoenix/router.ex:261: Callme.Router.dispatch/2
Jul 28 10:28:37 callmemd app/web.1: (callme) web/router.ex:1: Callme.Router.do_call/2
Jul 28 10:28:37 callmemd app/web.1: (callme) lib/callme/endpoint.ex:1: Callme.Endpoint.phoenix_pipeline/1
Jul 28 10:28:37 callmemd app/web.1: (callme) lib/phoenix/endpoint/render_errors.ex:34: Callme.Endpoint.call/2
一个问题是亚马逊ID的变量被错误地命名为我更改的系统变量。现在我正在尝试使用如下控制器代码:
def create(conn, %{"doctor" => doctor_params}) do
changeset = Doctor.changeset(%Doctor{}, doctor_params)
avatar = doctor_params["avatar"]
Callme.Avatar.store({%Plug.Upload{}, id: avatar.id})