2016-11-27 55 views
1

我現在用的是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 
}) 

存儲有什麼方法可以讓我指定我的文件自定義名稱和文件類型上傳?在我的情況下,它應該是一個圖像。

回答

2

您可以通過選項來上傳方法:

var options = { 
    destination: 'my-image-name.jpg', 
    metadata: { 
    contentType: 'image/jpeg', 
    } 
}; 

bucket.upload(req.file.path, options, function(err, file) { 
}); 
+0

謝謝!與創建寫入流而不是使用上傳方法相比,是否有任何優勢? – user3642173

+0

'bucket.upload'只是一個方便的方法,它會執行'createWriteStream' [檢查文檔API](https://googlecloudplatform.github.io/google-cloud-node/#/docs/storage/0.5.0/ storage/file?method = createWriteStream) – Erevald

+0

明白了 - 謝謝 – user3642173