我希望使用NodeJS上传图像并保存到用户记录,就像Rails的Paperclip gem一样。
我认为应该按照以下步骤进行,但我对此包的工作原理感到非常困惑:
- 接收图像并通过Paperclip进行大小调整
- 保存或更新到S3
- 将文件保存到数据库中的
user
我有一个Rails Postgres数据库,用户可以上传存储在S3中并由Paperclip gem重新格式化的图像。以下是它的存储方式:
irb(main):003:0> user.avatar
=> #<Paperclip::Attachment:0x000055b3e043aa50 @name=:avatar,
@name_string="avatar", @instance=#<User id: 1, email:
"example@gmail.com", created_at: "2016-06-11 22:52:36",
updated_at: "2019-06-16 17:17:16", first_name: "Clarissa",
last_name: "Jones", avatar_file_name: "two_people_talking.gif",
avatar_content_type: "image/gif", avatar_file_size: 373197,
avatar_updated_at: "2019-06-16 17:17:12", @options={:convert_options=>{},
:default_style=>:original, :default_url=>":style/missing.png",
:escape_url=>true, :restricted_characters=>/[&$+,\/:;=?@<>\[\]\
{\}\|\\\^~%# ]/, :filename_cleaner=>nil,
:hash_data=>":class/:attachment/:id/:style/:updated_at",
:hash_digest=>"SHA1", :interpolator=>Paperclip::Interpolations,
:only_process=>[],
:path=>"/:class/:attachment/:id_partition/:style/:filename",
:preserve_files=>false, :processors=>[:thumbnail],
:source_file_options=>{:all=>"-auto-orient"}, :storage=>:s3,
:styles=>{:large=>"500x500#", :medium=>"200x200#",
:thumb=>"100x100#"}, :url=>":s3_path_url",
:url_generator=>Paperclip::UrlGenerator,
:use_default_time_zone=>true, :use_timestamp=>true, :whiny=>true,
:validate_media_type=>true, :adapter_options=>
{:hash_digest=>Digest::MD5},
:check_validity_before_processing=>true, :s3_host_name=>"s3-us-
west-2.amazonaws.com", :s3_protocol=>"https", :s3_credentials=>
{:bucket=>"example", :access_key_id=>"REDACTED",
:secret_access_key=>"REDACTED",
:s3_region=>"us-west-2"}}, @post_processing=true,
@queued_for_delete=[], @queued_for_write={}, @errors={},
@dirty=false, @interpolator=Paperclip::Interpolations,
@url_generator=#<Paperclip::UrlGenerator:0x000055b3e043a8e8
@attachment=#<Paperclip::Attachment:0x000055b3e043aa50 ...>>,
@source_file_options={:all=>"-auto-orient"}, @whiny=true,
@s3_options={}, @s3_permissions={:default=>:"public-read"},
@s3_protocol="https", @s3_metadata={}, @s3_headers={},
@s3_storage_class={:default=>nil},
@s3_server_side_encryption=false, @http_proxy=nil,
@use_accelerate_endpoint=nil>
user.avatar(:thumb)
返回:
https://s3-us-west-2.amazonaws.com/example/users/avatars/000/000/001/thumb/two_people_talking.gif?1560705432
现在,我正在尝试允许用户通过react-native应用程序上传新的/更改图像,后端是Nodejs,这对我来说相对较新。
我非常困惑如何实现这一点,特别是因为所有示例都引用了我没有使用的Mongoose。
为了展示如何成功更新用户,这里是如何更新用户的first_name
:
users.updateUserPhoto = (req, res) => {
let id = req.decoded.id
let first_name = req.body.first_name
models.Users.update(
first_name: first_name,
{
where: {
id: req.decoded.id
}
},
).then(response => {
res.status(200).json({ status: 200, data: { response } });
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
})
}
我找到了这个包node-paperclip-s3,这是我要做的事情:
'use strict'
let users = {};
const { Users } = require('../models');
let models = require("../models/index");
let Sequelize = require('sequelize');
let Paperclip = require('node-paperclip');
let Op = Sequelize.Op;
let sequelizeDB = require('../modules/Sequelize');
users.updateUserPhoto = (req, res) => {
let id = req.decoded.id
let avatar = req.body.avatar <- this is a file path
models.Users.plugin(Paperclip.plugins, {
avatar: {
styles: [
{ original: true },
{ large: { width: 500, height: 500 } },
{ medium: { width: 200, height: 200 } },
{ thumb: { width: 100, height: 100 } }
],
prefix: '/users/{{attachment}}/{{id}}/{{filename}}',
name_format: '{{style}}.{{extension}}',
storage: 's3',
s3: {
bucket: process.env.S3_BUCKET_NAME,
region: 'us-west-2',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
}
}
})
models.Users.update(
avatar,
{
where: {
id: req.decoded.id
}
},
).then(response => {
res.status(200).json({ status: 200, data: { response } });
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
})
}
我也尝试过类似以下的方式:
models.Users.update(Paperclip.plugins, {
avatar: {
styles: [
{ original: true },
{ large: { width: 500, height: 500 } },
{ medium: { width: 200, height: 200 } },
{ thumb: { width: 100, height: 100 } }
],
prefix: '/users/{{attachment}}/{{id}}/{{filename}}',
name_format: '{{style}}.{{extension}}',
storage: 's3',
s3: {
bucket: process.env.S3_BUCKET_NAME,
region: 'us-west-2',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
}
},
{
where: {
id: req.decoded.id
}
},
).then(response => {
res.status(200).json({ status: 200, data: { response } });
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
})
})
我已经尝试过:
let new_avatar = (Paperclip.plugins, {
avatar: {
styles: [
{ original: true },
{ large: { width: 500, height: 500 } },
{ medium: { width: 200, height: 200 } },
{ thumb: { width: 100, height: 100 } }
],
prefix: `/users/avatars/{{attachment}}/{{id}}/{{filename}}`,
name_format: '{{style}}.{{extension}}',
storage: 's3',
s3: {
bucket: process.env.S3_BUCKET_NAME,
region: 'us-west-2',
key: process.env.AWS_ACCESS_KEY_ID,
secret: process.env.AWS_SECRET_ACCESS_KEY,
}
},
})
let data = {
avatar: new_avatar
}
models.Users.update(
data,
{
where: {
id: req.decoded.id
}
},
).then(response => {
res.status(200).json({ status: 200, data: { response } });
})
.catch(error => {
res.status(500).json({ status: 500, err: error });
})
从上面链接中的示例中,我不理解它是如何保存到S3的,也不知道它是如何以与Rails gem创建记录相同的方式更新数据库的。
问题:如何以与Rails paperclip gem相同的方式保存调整大小后的图像+原始图像到S3和user
记录中的数据库。
我最初为此打开了一个400分的赏金,并非常乐意向任何帮助我解决这个问题的人提供400分的奖励。谢谢!
models.Users.plugin
是未定义的? - 鄭脈龍node-paperclip
的作者表示:“它目前只适用于 mongoose,但已经设置好了以便轻松扩展到其他数据库。” [repo] (https://github.com/ballantyne/node-paperclip#node-paperclip) - 鄭脈龍