上传角度为6 s3和多方的图像

时间:2018-08-07 16:43:01

标签: node.js angular amazon-s3 image-uploading multiparty

我正在做一个小项目,在S3中上传图片时遇到问题,我有以下代码:

var AWS = require('aws-sdk');
AWS.config.loadFromPath('/Users/user1/.aws/credentials.json');

var http = require('http');
var util = require('util');
var multiparty = require('multiparty');

//component class
  addAds(event){
    //console.log('Hola fuck1');
    event.preventDefault();
    //console.log('Hola add');
    const newAd: CreateAds = {
      title: this.title,
      description: this.description,
      celphone: this.celPhone,
      published: this.published
    };
    this.createService.addAds(newAd).subscribe(CreateAds => {
      console.log(newAd);
      this.Ads.push(CreateAds);
      this.title = '';
      this.description = '';
      this.celPhone = '';
      console.log(this.Ads);
    });
  }
  
//service class
addAds(newAd: CreateAds){
    return this.http.post<CreateAds>(`${this.domain}/api/ads/ads`,newAd).pipe(map(res => res));
  } 
 
//router class
 router.post('/ads',(req,res,next) => {
    console.log(req.body);
    const ad = req.body; 
    var form = new multiparty.Form();     
    form.parse(req, function(err, fields, files) {
        console.log('Un error: ' + err);
        
        var s3 = new AWS.S3({
            logger: console
        });    
        console.log(files.upload[0].path);
        let fileData = fs.readFileSync(files.upload[0].path);                                                
        console.log(fileData);
        var params = {
        Bucket: 'lobeliaphotosprod',
        Key: 'foto1.jpg',
        Body: fileData,
        ACL:'public-read'
        };
        s3.putObject(params, function (perr, pres) {
            if (perr) {
                console.log("Error uploading image: ", perr);                                                
            } else {
        console.log("uploading image successfully");                        
            }                  
        });                                                      return;
    });
});
<form (submit)="addAds($event)" id="idforms"  enctype="multipart/form-data">
        <div>
          <div class="row form-group centered">
            <h1>Ingresa tu anuncio.</h1>
          </div>
          <div class="row form-group">
            <input type="text" name="Titulo" [(ngModel)]="title" placeholder="Título" class="form-control">
          </div>
          <div class="row form-group">
            <textarea name="Descripcion" rows="9" form="idforms" [(ngModel)]="description" placeholder="Descripción" class="form-control"></textarea>
          </div>
          <div class="row form-group">
            <input type="text" name="Celular" [(ngModel)]="celPhone" placeholder="Celular" class="form-control">
          </div>
          <div class="row form-group">
            <input type="file" name="fotos" multiple="multiple" [(ngModel)]="pictures" class="form-control">
          </div>
          <div class="row">
            <span class="input-group-addon">
              <button type="submit" class="btn btn-primary">
                  Agregar
              </button>
            </span>
        </div>
        </div>
      </form>

我有这个错误:

[nodemon]由于更改而重新启动... [nodemon]开始node src/index.js 在端口3000上服务 {发布:假} 联合国错误:UnsupportedMediaTypeError:不受支持的内容类型

    console.log(files.upload[0].path);
                      ^

TypeError:无法读取未定义的属性“ upload”     

如果您能给予我任何帮助,我将不胜感激。

谢谢!。

0 个答案:

没有答案