【发布时间】:2015-08-14 20:01:44
【问题描述】:
我正在尝试使用节点 AWS 开发工具包上传图像,但在使用循环时遇到问题。
这是我的节点模块:
gm = require('gm').subClass imageMagick: true
s3 = require '../../modules/s3'
Photo = require '../../models/Photo'
sizes =
t: 100
m: 240
z: 640
l: 1024
k: 2048
newPhoto = new Photo
newPhotoImg = gm process.cwd() + '/' + req.files.file.path
for key, val of sizes
newPhotoImg
.resize(null, val)
.toBuffer 'jpg', (err, buffer) ->
if err then console.error err and res.status(500).end() else
params =
Key: newPhoto.id + '-' + key + '.jpg'
Body: buffer
ContentType: 'image/jpeg'
s3.putObject params, (err, data) ->
if err then console.error err else console.log data
我的 s3 模块运行良好,很可能不是问题的一部分。
问题是我为每种尺寸返回了一个 ETag,但是当我查看我的 S3 管理控制台时,只存储了循环的最后一项(尺寸为“k”)。
有人可以帮帮我吗?
【问题讨论】:
-
似乎您正在以错误的方式执行异步循环,请为此使用
async库。此处描述的类似问题stackoverflow.com/questions/21184340/async-for-loop-in-node-js -
@RistoNovik 它适用于
async.forEachOf。谢谢
标签: node.js amazon-web-services amazon-s3 coffeescript aws-sdk