【问题标题】:using multer to post files into Digital Ocean Space使用 multer 将文件发布到 Digital Ocean Space
【发布时间】:2018-11-11 20:17:53
【问题描述】:

尝试将本地文件发布到 Digital Ocean Space

我的帖子请求正文是:

[ 'http://localhost:8090/d/3534352009.png',
  'http://localhost:8090/d/3534352009-600x600.png' ]

这些文件位于运行此 API 的本地/

在 JS 文件中我有配置

// Change bucket property to your Space name
const upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'xxxx',
        acl: 'public-read',
        key: function (request, file, cb) {
            console.log(file);
            cb(null, file.originalname);
        }
    })
}).array('upload', 2);

还有发帖途径

app.post('/upload', function (request, response, next) {
    upload(request, response, function (error) {
        if (error) {
            console.log(error);
            return response.redirect("/error");
        }
        console.log('File uploaded successfully.');
        response.redirect("/success");
    });
});

不知道如何将这两张图片从一个对象发布到数字海洋空间

有很多关于如何从 FORM 浏览器用户发布多张图片的教程,但我在本地获取文件,我想通过脚本发布。

【问题讨论】:

    标签: javascript express digital-ocean multer


    【解决方案1】:

    可以使用 aws sdk putObject API 来完成。这对我有用。

    const aws = require('aws-sdk');
    var fs =  require('fs');
    
    const spacesEndpoint = new aws.Endpoint('sgp1.digitaloceanspaces.com');
    const spaces = new aws.S3({
    endpoint: spacesEndpoint,
    accessKeyId: 'spaces_key',
    secretAccessKey: 'spaces_secret'
      });
    
      fs.readFile('file_path', function (err, data) {
      if (err) { throw err; }
    
    
      var params = {Bucket: 'bucket_name', Key:'file_name', Body: data };
    
      spaces.putObject(params, function(err, data) {
    
         if (err) {
    
             console.log(err)
    
         } else {
    
             console.log("Successfully uploaded data to DO speaces");
    
          }
    
         });
    
         });
    

    ##如果您想将文件路径作为请求正文发送,请在 API 中包含此代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-18
      • 2022-12-22
      • 1970-01-01
      • 2019-08-13
      • 2018-03-19
      • 2023-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多