0
이전에 다운로드 한 파일을 저장하기 위해 Ecto 및 Amazon S3가있는 엘릭서 아크를 사용하고 있습니다. 모든 것이 제대로 작동하는 것으로 보입니다. 결국 S3에서 끝납니다. 그러나 아무 것도 내 데이터베이스에 저장되지 않습니다. 따라서 URL을 생성하려고하면 항상 기본 URL 만 반환됩니다.엘릭서 아크 : 파일이 업로드되고 처리되지만 데이터베이스에 저장되지 않습니다.
iex > user = Repo.get(User, 3)
iex > Avatar.store({"/tmp/my_file.png", user})
{:ok, "my_file.png"}
그러나 user.avatar
필드가 아직 전무하다 :
이 내가 파일을 저장하는 방법입니다.
내 사용자 모듈 :
defmodule MyApp.User do
use MyApp.Web, :model
use Arc.Ecto.Schema
alias MyApp.Repo
schema "users" do
field :name, :string
field :email, :string
field :avatar, MyApp.Avatar.Type
embeds_many :billing_emails, MyApp.BillingEmail
embeds_many :addresses, MyApp.Address
timestamps
end
@required_fields ~w(name email)
@optional_fields ~w(avatar)
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
|> cast_embed(:billing_emails)
|> cast_embed(:addresses)
|> validate_required([:name, :email])
|> validate_format(:email, ~r/@/)
|> unique_constraint(:email)
|> cast_attachments(params, [:avatar])
end
end
아바타 업 로더 :
defmodule MyApp.Avatar do
use Arc.Definition
# Include ecto support (requires package arc_ecto installed):
use Arc.Ecto.Definition
@acl :public_read
# To add a thumbnail version:
@versions [:original, :thumb]
# Whitelist file extensions:
def validate({file, _}) do
~w(.jpg .jpeg .gif .png) |> Enum.member?(Path.extname(file.file_name))
end
# Define a thumbnail transformation:
def transform(:thumb, _) do
{:convert, "-strip -thumbnail 250x250^ -gravity center -extent 250x250 -format png", :png}
end
def transform(:original, _) do
{:convert, "-format png", :png}
end
def filename(version, {file, scope}), do: "#{version}-#{file.file_name}"
# Override the storage directory:
def storage_dir(version, {file, scope}) do
"uploads/user/avatars/#{scope.id}"
end
# Provide a default URL if there hasn't been a file uploaded
def default_url(version, scope) do
"/images/avatars/default_#{version}.png"
end
end