So I have this function from Google Cloud Storage that I needed to change so it can upload multiple files instead of one. I'm trying to find a good solution to make it always wait until all files are uploaded. Not sure how to make it async - should I do await on stream.on('finish',(...))
or on file.makePublic().then(...)
which definitely is a Promiste that I could collect with Promise.all()
and then resolve next()
.
Or if there is already solution for that that Google didn't disclosed in their docs it would be even better.
Function:
function sendUploadsToGCS (req, res, next) {
if (!req.files) {
return next()
}
let vals = Object.values(req.files)
for(let f of vals){
const gcsname = Date.now() + f[0].originalname
const file = bucket.file(gcsname)
const stream = file.createWriteStream({
metadata: {
contentType: f[0].mimetype
},
resumable: false
})
stream.on('error', (err) => {
f[0].cloudStorageError = err
next(err)
})
stream.on('finish', () => {
f[0].cloudStorageObject = gcsname;
file.makePublic().then(() => {
f[0].cloudStoragePublicUrl = getPublicUrl(gcsname)
console.log('pub url: ', getPublicUrl(gcsname))
next()
})
})
stream.end(f[0].buffer)
}
}
Original function (for one file): https://cloud.google.com/nodejs/getting-started/using-cloud-storage#upload_to_cloud_storage
This how I resolved it:
function sendUploadsToGCS (req, res, next) {
if (!req.files) {
return next()
}
let promises = []
let vals = Object.values(req.files)
for(let f of vals){
const gcsname = Date.now() + f[0].originalname
const file = bucket.file(gcsname)
const stream = file.createWriteStream({
metadata: {
contentType: f[0].mimetype
},
resumable: false
})
stream.on('error', (err) => {
f[0].cloudStorageError = err
next(err)
})
stream.end(f[0].buffer)
promises.push(
new Promise ((resolve, reject) => {
stream.on('finish', () => {
f[0].cloudStorageObject = gcsname;
file.makePublic().then(() => {
f[0].cloudStoragePublicUrl = getPublicUrl(gcsname)
resolve()
})
})
})
)
}
Promise.all(promises).then(() => next())
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.