通过Node.js将base64编码映像上传到Amazon S3 [英] Uploading base64 encoded Image to Amazon S3 via Node.js

查看:589
本文介绍了通过Node.js将base64编码映像上传到Amazon S3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我做了一个深夜编码会话,并创建了一个小的node.js / JS(实际上是CoffeeScript,但CoffeeScript只是JavaScript,所以让我们说JS)应用程序。



目标是什么:


  1. 客户端通过socket.io向服务器发送画布数据(png)

  2. 服务器将图片上传到amazon s3

步骤1已完成。



服务器现在有一个字符串a

  data:image / png; base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACt ... 

我的问题是:我的下一步是流并在那里创建实际图片?



knox https:// github .com / LearnBoost / knox 看起来像一个令人敬畏的lib PUT东西到S3,但我缺少的是base64-encoded-image-string和实际上传操作之间的粘连



很多,

解决方案

对于仍然在努力解决这个问题的人。这是我使用原生aws-sdk的方法。

  var AWS = require('aws-sdk'); 
AWS.config.loadFromPath('./ s3_config.json');
var s3Bucket = new AWS.S3({params:{Bucket:'myBucket'}});

您的路由器方法: -
ContentType应设置为图像的内容类型档案

  buf = new Buffer(req.body.imageBinary.replace(/ ^ data:image\ / \w + base64,/),'base64')
var data = {
Key:req.body.userId,
Body:buf,
ContentEncoding:'base64'
ContentType:'image / jpeg'
};
s3Bucket.putObject(data,function(err,data){
if(err){
console.log(err);
console.log ',data);
} else {
console.log('succeeded uploaded the image!');
}
});

s3_config.json文件是: -

  {
accessKeyId:xxxxxxxxxxxxxxxx,
secretAccessKey:xxxxxxxxxxxxxx,
region:us-east-1
}


yesterday i did a deep night coding session and created a small node.js/JS (well actually CoffeeScript, but CoffeeScript is just JavaScript so lets say JS) app.

what's the goal:

  1. client sends a canvas datauri (png) to server (via socket.io)
  2. server uploads image to amazon s3

step 1 is done.

the server now has a string a la

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACt...

my question is: what are my next steps to "stream"/upload this data to Amazon S3 and create an actual image there?

knox https://github.com/LearnBoost/knox seems like an awesome lib to PUT something to S3, but what i'm missing is the glue between the base64-encoded-image-string and actual upload action?

any ideas, pointers and feedback welcome.

thx a lot, franz

解决方案

For people who are still struggling with this issue. Here is the approach I used with native aws-sdk.

var AWS = require('aws-sdk');
AWS.config.loadFromPath('./s3_config.json');
var s3Bucket = new AWS.S3( { params: {Bucket: 'myBucket'} } );

inside your router method :- ContentType should be set to the content type of the image file

  buf = new Buffer(req.body.imageBinary.replace(/^data:image\/\w+;base64,/, ""),'base64')
  var data = {
    Key: req.body.userId, 
    Body: buf,
    ContentEncoding: 'base64',
    ContentType: 'image/jpeg'
  };
  s3Bucket.putObject(data, function(err, data){
      if (err) { 
        console.log(err);
        console.log('Error uploading data: ', data); 
      } else {
        console.log('succesfully uploaded the image!');
      }
  });

s3_config.json file is:-

{
  "accessKeyId":"xxxxxxxxxxxxxxxx",
  "secretAccessKey":"xxxxxxxxxxxxxx",
  "region":"us-east-1"
}

这篇关于通过Node.js将base64编码映像上传到Amazon S3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆