Ask Your Question
1

Is it not possible to obtain a signed URL for images that are stored in Google Cloud Storage when using a Node.js server on Cloud Run?

asked 2023-06-05 11:21:52 +0000

pufferfish gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-06-05 11:42:02 +0000

lalupa gravatar image

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-05 11:21:52 +0000

Seen: 2 times

Last updated: Jun 05 '23