Upload file to google cloud storage with NodeJS

前端 未结 2 2025
盖世英雄少女心
盖世英雄少女心 2021-01-02 18:53

My upload.js file contains the following code:

module.exports = {

    up: function () {
        const storage = require(\'@google-cloud/storage\');         


        
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-02 19:03

    I would recommend moving your requires to the first line.

    const storage = require('@google-cloud/storage');
    const fs = require('fs');
    
    module.exports = {
        up: function () {
            const gcs = storage({
                projectId: 'MY_PROJECT_ID',
                keyFilename: './service-account.json'
            });
            const bucket = gcs.bucket('MY_BUCKET');
            bucket.upload('picture.jpg', function(err, file) {
                if (err) throw new Error(err);
            });
        },
    }
    

    I would also console.log(storage) and confirm it is defined.

提交回复
热议问题