Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

It is possible to obtain a signed URL for images that are stored in Google Cloud Storage when using a Node.js server on Cloud Run.

To obtain a signed URL, you can use the Google Cloud Storage client library for Node.js. Here's an example:

const { Storage } = require('@google-cloud/storage');

// Creates a client
const storage = new Storage();

// Bucket name where the image is stored
const bucketName = 'my-bucket';

// Path to the image file
const filePath = 'path/to/image.png';

// Expiration time for the signed URL (in seconds)
const expiration = 300; // 5 minutes

// Generate a signed URL
const [url] = await storage.bucket(bucketName).file(filePath).getSignedUrl({
  action: 'read',
  expires: Date.now() + expiration * 1000, // convert expiration to milliseconds
});

// Use the signed URL to access the image
console.log(url); // e.g. https://storage.googleapis.com/my-bucket/path/to/image.png?Expires=xxxx&GoogleAccessId=xxxx&Signature=xxxx

Note that the signed URL is time-limited and can only be used for the specified action (in this case, "read"). You can also specify additional options such as headers and content type when generating the signed URL.