This code defines an HTTP function named getSignedUrl
that generates a signed URL for a file in Google Cloud Storage, allowing a user to upload a file to the specified bucket and file name. The function takes a POST request, retrieves the user's authorization (TODO), and creates a temporary upload URL with a 5-minute expiration time.
npm run import -- "sign a file url for uploading to google storage"
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
/**
* HTTP function that generates a signed URL
* The signed URL can be used to upload files to Google Cloud Storage (GCS)
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.getSignedUrl = (req, res) => {
if (req.method !== 'POST') {
// Return a "method not allowed" error
return res.status(405).end();
}
// TODO(developer) check that the user is authorized to upload
// Get a reference to the destination file in GCS
const file = storage.bucket(req.body.bucket).file(req.body.filename);
// Create a temporary upload URL
const expiresAtMs = Date.now() + 300000; // Link expires in 5 minutes
const config = {
action: 'write',
expires: expiresAtMs,
contentType: req.body.contentType,
};
file.getSignedUrl(config, (err, url) => {
if (err) {
console.error(err);
res.status(500).end();
return;
}
res.send(url);
});
};
const { Storage } = require('@google-cloud/storage');
/**
* HTTP function that generates a signed URL
* The signed URL can be used to upload files to Google Cloud Storage (GCS)
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.getSignedUrl = async (req, res) => {
if (req.method!== 'POST') {
return res.status(405).send({ message: 'Method not allowed' });
}
// Check user authorization
if (!req.body.authorization) {
return res.status(401).send({ message: 'Unauthorized' });
}
try {
// Get a reference to the destination file in GCS
const file = await storage.bucket(req.body.bucket).file(req.body.filename).getMetadata();
if (!file) {
return res.status(404).send({ message: 'File not found' });
}
// Generate a signed URL
const expiresAtMs = Date.now() + 300000; // Link expires in 5 minutes
const signedUrl = await file.getSignedUrl({
action: 'write',
expires: expiresAtMs,
contentType: req.body.contentType,
});
res.send(signedUrl);
} catch (error) {
console.error(error);
res.status(500).send({ message: 'Internal Server Error' });
}
};
const { Storage } = require('@google-cloud/storage');
const storage = new Storage();
exports.getSignedUrl = (req, res) => {
//...
}
getSignedUrl
that takes two parameters: req
and res
, which represent the request and response contexts.if (req.method!== 'POST') {
return res.status(405).end();
}
// TODO(developer) check that the user is authorized to upload
const file = storage.bucket(req.body.bucket).file(req.body.filename);
const expiresAtMs = Date.now() + 300000; // Link expires in 5 minutes
const config = {
action: 'write',
expires: expiresAtMs,
contentType: req.body.contentType,
};
file.getSignedUrl(config, (err, url) => {
if (err) {
console.error(err);
res.status(500).end();
return;
}
res.send(url);
});
getSignedUrl
method on the file reference. If an error occurs, it logs the error and returns a 500 Internal Server Error response. Otherwise, it sends the signed URL as a response.