如何使用多节点 js 将图像上传到 Google 云
How to upload image to Google Cloud with multer node js
如果存储是 memoryStorage,我已经在 NodeJS 中使用 Multer 完成上传,因为文件首先保存在缓冲区中,然后我可以从缓冲区上传到 Google 驱动器,
但是如果使用 memoryStorage 我不能重命名图像文件,
我用的是multer.diskStorage,但是当我post的时候,文件上传成功了,但是图片没有上传,文件变小了,只有10B。
这是我在 helper 中的代码,具有 uploadImage 功能
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name
const { format } = util
const uploadImage = (file) => new Promise((resolve, reject) => {
console.log(file);
//const { originalname, buffer } = file
const { filename, destination } = file
//const blob = bucket.file(originalname.replace(/ /g, "_"))
const blob = bucket.file(filename)
const blobStream = blob.createWriteStream({
resumable: false
})
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
)
resolve(publicUrl)
})
.on('error', () => {
reject(`Unable to upload image, something went wrong`)
})
//.end(buffer)
.end(destination)
})
module.exports = uploadImage
使用上面的代码我成功上传到 Google 驱动器但不是图片,因为大小总是 10B。
本例中图片上传到temp或者本地任意文件夹后,我们就可以上传到google云端
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name di google drive
const path = require('path')
const { format } = util
// promises are built right away, so there's no need for then to resolve and catch for errors
const uploadImage = (file) => new Promise((resolve, reject) => {
//console.log(file);
const {filename} = file;
const picture = path.join(__dirname,'../uploads/',filename);
// This is the upload command
bucket.upload(picture);
// This is sent to return
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${filename}`
)
resolve(publicUrl)
reject(err=>(err))
})
module.exports = uploadImage
如果存储是 memoryStorage,我已经在 NodeJS 中使用 Multer 完成上传,因为文件首先保存在缓冲区中,然后我可以从缓冲区上传到 Google 驱动器,
但是如果使用 memoryStorage 我不能重命名图像文件,
我用的是multer.diskStorage,但是当我post的时候,文件上传成功了,但是图片没有上传,文件变小了,只有10B。
这是我在 helper 中的代码,具有 uploadImage 功能
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name
const { format } = util
const uploadImage = (file) => new Promise((resolve, reject) => {
console.log(file);
//const { originalname, buffer } = file
const { filename, destination } = file
//const blob = bucket.file(originalname.replace(/ /g, "_"))
const blob = bucket.file(filename)
const blobStream = blob.createWriteStream({
resumable: false
})
blobStream.on('finish', () => {
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${blob.name}`
)
resolve(publicUrl)
})
.on('error', () => {
reject(`Unable to upload image, something went wrong`)
})
//.end(buffer)
.end(destination)
})
module.exports = uploadImage
使用上面的代码我成功上传到 Google 驱动器但不是图片,因为大小总是 10B。
本例中图片上传到temp或者本地任意文件夹后,我们就可以上传到google云端
const util = require('util')
const gc = require('../config/')
const bucket = gc.bucket('jsimage')//bucket name di google drive
const path = require('path')
const { format } = util
// promises are built right away, so there's no need for then to resolve and catch for errors
const uploadImage = (file) => new Promise((resolve, reject) => {
//console.log(file);
const {filename} = file;
const picture = path.join(__dirname,'../uploads/',filename);
// This is the upload command
bucket.upload(picture);
// This is sent to return
const publicUrl = format(
`https://storage.googleapis.com/${bucket.name}/${filename}`
)
resolve(publicUrl)
reject(err=>(err))
})
module.exports = uploadImage