【问题标题】:Attributes for image stored using Google Cloud Storage使用 Google Cloud Storage 存储的图像的属性
【发布时间】:2017-04-11 10:26:54
【问题描述】:

我正在使用NPM documentation 将图像上传到谷歌云存储

app.post('/test', upload.single('image'), function (req, res, next) {
    });
    let bucket = gcs.bucket('bucket-name');

    bucket.upload(req.file.path, function(err, file) {
        if (err) {
            throw new Error(err);
        }
        console.log(file);
    });

    console.log(req.file);

    // req.body will hold the text fields, if there were any
})

有什么方法可以为我上传的文件指定自定义名称和文件类型?就我而言,它应该是一张图片。

【问题讨论】:

    标签: node.js google-app-engine google-cloud-storage


    【解决方案1】:

    您可以将选项传递给您的上传方法:

    var options = {
      destination: 'my-image-name.jpg',
      metadata: {
        contentType: 'image/jpeg',
      }
    };
    
    bucket.upload(req.file.path, options, function(err, file) {
    });
    

    【讨论】:

    • 谢谢!创建 writestream 而不是使用 upload 方法有什么好处吗?
    • bucket.upload 只是一个方便的方法,可以做到createWriteStream check documentation api
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 2020-01-17
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2019-12-02
    相关资源
    最近更新 更多