简体   繁体   English

自动将图像上传到s3

[英]Upload an image automatically to s3

I have an upload form that is currently working where the user choose his/her desired avatar/picture. 我有一个正在上载的上传表单,用户可以在其中选择他/她所需的头像/图片。 But now I'm integrating facebook api and upon successful registration using FB Javascript SDK, I will then have to upload the user facebook profile pic into our S3. 但是,现在我正在集成facebook api,并使用FB Javascript SDK成功注册后,我将不得不将用户facebook个人资料图片上传到我们的S3中。

<img src="https://fullpath/mypic.png" id="image"/>

I've researched around on converting the image to blob or something. 我已经研究过将图像转换为斑点或其他东西。 So how do I automatically upload mypic.png to my s3? 那么,如何自动将mypic.png上传到我的s3? Please any help is appreciated. 请任何帮助表示赞赏。

Use the AWS JS SDK. 使用AWS JS SDK。

Here is the getting started guide: http://aws.amazon.com/developers/getting-started/browser/ 这是入门指南: http : //aws.amazon.com/developers/getting-started/browser/

It's actually quite simple once you get the roles setup. 一旦完成角色设置,这实际上就非常简单。

No server side code needed! 无需服务器端代码!

Of course you will need to get the image data first like this: 当然,您将需要像这样首先获取图像数据:

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
canvas.width = 512;
canvas.height = 512;
context.drawImage(img, 0, 0, 512, 512);
var data = canvas.toDataURL('image/webp', 0.5); //webp and 50% compression

var params = {
  Key:Date.now().toString(), //example
  ContentType:'text/plain', //sending a base64 text string
  Body:data //the base64 string is now the body
};

bucket.putObject(params, function(err, data) { ... });

you can't do that from the browser, you need a server side script which will receive your upload and send it to s3. 您无法通过浏览器执行此操作,您需要一个服务器端脚本来接收您的上传并将其发送到s3。 which framework do you use on the server side? 您在服务器端使用哪种框架?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM