Created
September 4, 2022 13:10
-
-
Save edwardsmoses/170e01693a08809bc41bf62812dcf48c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2017, Google, Inc. | |
// Licensed under the Apache License, Version 2.0 (the "License"); | |
// you may not use this file except in compliance with the License. | |
// You may obtain a copy of the License at | |
// | |
// http://www.apache.org/licenses/LICENSE-2.0 | |
// | |
// Unless required by applicable law or agreed to in writing, software | |
// distributed under the License is distributed on an "AS IS" BASIS, | |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
// See the License for the specific language governing permissions and | |
// limitations under the License. | |
'use strict'; | |
const config = require('../config'); | |
const {Storage} = require("@google-cloud/storage") | |
const storage = new Storage({ | |
projectId: config.get('GCLOUD_PROJECT') | |
}) | |
const GCLOUD_BUCKET = config.get('GCLOUD_BUCKET'); | |
const bucket = storage.bucket(GCLOUD_BUCKET); | |
// Express middleware that will automatically pass uploads to Cloud Storage. | |
// req.file is processed and will have a new property: | |
// * ``cloudStoragePublicUrl`` the public url to the object. | |
// [START sendUploadToGCS] | |
function sendUploadToGCS(req, res, next) { | |
// The existing code in the handler checks to see if there | |
// is a file property on the HTTP request - if a file has | |
// been uploaded, then Multer will have created this | |
// property in the preceding middleware call. | |
if (!req.file) { | |
return next(); | |
} | |
// In addition, a unique object name, oname, has been | |
// created based on the file's original name. It has a | |
// prefix generated using the current date and time. | |
// This should ensure that a new file upload won't | |
// overwrite an existing object in the bucket | |
const oname = Date.now() + req.file.originalname; | |
//reference to the file object.. | |
const file = bucket.file(oname); | |
// stream to write the file to google cloud storage | |
const stream = file.createWriteStream({ | |
metadata: { | |
contentType: req.file.mimetype, | |
} | |
}); | |
stream.on('finish', () => { | |
file.makePublic().then(() => { | |
req.file.cloudStoragePublicUrl = `https://storage.googleapis.com/${GCLOUD_BUCKET}/${oname}`; | |
next(); | |
}) | |
}); | |
stream.on('error', (err) => { | |
next(err); | |
}) | |
stream.end(req.file.buffer); | |
} | |
// [END sendUploadToGCS] | |
// Multer handles parsing multipart/form-data requests. | |
// This instance is configured to store images in memory. | |
// This makes it straightforward to upload to Cloud Storage. | |
// [START multer] | |
const Multer = require('multer'); | |
const multer = Multer({ | |
storage: Multer.MemoryStorage, | |
limits: { | |
fileSize: 40 * 1024 * 1024 // no larger than 40mb | |
} | |
}); | |
// [END multer] | |
module.exports = { | |
sendUploadToGCS, | |
multer | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment