Upload a file to Google Cloud, in a specific directory

前端 未结 6 1657
借酒劲吻你
借酒劲吻你 2021-02-04 04:31

How to upload a file on Google Cloud, in a specific bucket directory (e.g. foo)?

\"use strict\";

const gcloud = require(\"gcloud\");

const PROJECT         


        
6条回答
  •  离开以前
    2021-02-04 05:02

    UPDATE 2020

    according to google documentation:

    const { Storage } = require('@google-cloud/storage');
    const storage = new Storage()
    const bucket = storage.bucket('YOUR_GCLOUD_STORAGE_BUCKET')
    const blob = bucket.file('youFolder/' + 'youFileName.jpg')
    
    const blobStream = blob.createWriteStream({
        resumable: false,
        gzip: true,
        public: true
    })
    
    blobStream.on('error', (err) => {
        console.log('Error blobStream: ',err)
    });
    
    blobStream.on('finish', () => {
    // The public URL can be used to directly access the file via HTTP.
        const publicUrl = ('https://storage.googleapis.com/'+ bucket.name + '/' + blob.name)
        res.status(200).send(publicUrl);
    });
    
    blobStream.end(req.file.buffer)//req.file is your original file
    

提交回复
热议问题