我正在关注使用Arc上传图片的https://github.com/stavro/arc指南。我正在尝试使用博客文章上传图像,但是我在控制台中收到以下错误,指出配置存在一些问题,但我不知道到底是什么。我之前从未使用过AWS S3,因此我对这方面的事情视而不见。
错误:
You tried to access the AWS EC2 instance meta, but it could not be reached.
This happens most often when trying to access it from your local computer,
which happens when environment variables are not set correctly prompting
ExAws to fallback to the Instance Meta.
Please check your key config and make sure they're configured correctly:
For Example:
```
ExAws.Config.new(:s3)
ExAws.Config.new(:dynamodb)
```
(ex_aws) lib/ex_aws/instance_meta.ex:20: ExAws.InstanceMeta.request/2
(ex_aws) lib/ex_aws/instance_meta.ex:52: ExAws.InstanceMeta.instance_role_credentials/1
(ex_aws) lib/ex_aws/instance_meta.ex:58: ExAws.InstanceMeta.security_credentials/1
(ex_aws) lib/ex_aws/config/auth_cache.ex:58: ExAws.Config.AuthCache.refresh_config/2
(ex_aws) lib/ex_aws/config/auth_cache.ex:33: ExAws.Config.AuthCache.handle_call/3
(stdlib) gen_server.erl:615: :gen_server.try_handle_call/4
(stdlib) gen_server.erl:647: :gen_server.handle_msg/5
我的代表:
defp deps do
[{:phoenix, "~> 1.3.0-rc"},
{:phoenix_pubsub, "~> 1.0"},
{:phoenix_ecto, "~> 3.2"},
{:postgrex, ">= 0.0.0"},
{:phoenix_html, "~> 2.6"},
{:phoenix_live_reload, "~> 1.0", only: :dev},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0"},
{:mailgun, "~> 0.1.2"},
{:arc_ecto, "~> 0.3.1"},
{:arc, "~> 0.8.0"},
{:ex_aws, "~> 1.1"},
{:sweet_xml, "~> 0.6"},
{:poison, "~> 3.1", override: true}]
end
config.ex:
config :arc,
storage: Arc.Storage.S3, # or Arc.Storage.Local
bucket: {:system, "iotcimages"}, # if using Amazon S3
virtual_host: true
config :ex_aws,
access_key_id: [{:system, "AWS_ACCESS_KEY_ID"}, :instance_role],
secret_access_key: [{:system, "AWS_SECRET_ACCESS_KEY"}, :instance_role],
region: "eu-west-1",
s3: [
scheme: "https://",
host: "s3.eu-west-1.amazonaws.com",
region: "eu-west-1"
]
在prod.secret.ex中:
config :ex_aws,
access_key_id: "****",
secret_access_key: "****"
将以下内容添加到endpoint.ex:
plug Plug.Static,
at: "/uploads", from: Path.expand('./uploads'), gzip: false
我在post.ex中的架构:
defmodule Iotc.Articles.Post do
use Ecto.Schema
use Arc.Ecto.Model
import Ecto.Changeset
alias Iotc.Articles.Post
schema "articles_posts" do
field :body, :string
field :mainImg, Iotc.Mainimage.Type, null: :true
field :slug, :string
field :snippet, :string
field :thumbImg, :string, null: :true
field :title, :string
timestamps()
end
@required_fields ~w()
@optional_fields ~w(body)
@required_file_fields ~w()
@optional_file_fields ~w(mainImage)
@required_fields ~w()
@optional_fields ~w(slug)
@required_fields ~w()
@optional_fields ~w(snippet)
@required_fields ~w()
@optional_fields ~w(title)
更新
我现在已将配置添加到dev.exs:
config :arc,
storage: Arc.Storage.S3, # or Arc.Storage.Local
bucket: {:system, "iotcimages"}, # if using Amazon S3
virtual_host: true
config :ex_aws,
access_key_id: [{:system, "AWS_ACCESS_KEY_ID"}, :instance_role],
secret_access_key: [{:system, "AWS_SECRET_ACCESS_KEY"}, :instance_role],
region: "eu-west-1",
s3: [
scheme: "https://",
host: "s3.eu-west-1.amazonaws.com",
region: "eu-west-1"
]
我已将我的密钥添加到.env
文件中:
export AWS_ACCESS_KEY_ID=myAccessKey
export AWS_SECRET_ACCESS_KEY=mySecretAccessKey
我在启动服务器之前运行了source .env
。
我可以正常加载页面,但是当我尝试在表单中上传图片时,我收到了一个错误。关键信息似乎是(RuntimeError) Instance Meta Error: {:error, %{reason: :ehostdown}}
从控制台我运行ExAws.S3.list_buckets
只返回下面的内容,所以我似乎根本没有连接到S3。
%ExAws.Operation.S3{body: "", bucket: "", headers: %{}, http_method: :get,
params: [], parser: &ExAws.S3.Parsers.parse_all_my_buckets_result/1, path: "/",
resource: "", service: :s3, stream_builder: nil}
答案 0 :(得分:0)
由于:system
使用bucket: {:system, "iotcimages"},
原子,因此您必须为该存储桶使用环境变量,如:
bucket: {:system, "AWS_S3_BUCKET"},
如果您不想使用环境变量,bucket: "my_aws_bucket_name
也可以使用