简体   繁体   中英

Resizing and Serving Images from Google Cloud Storage with Nodejs?

I found this link upload and resize the image with google storage . I look at the google document but seem to be google is not support for nodejs.

How can I implement the API on nodejs? or the way to just input google storage link and get output serve_url?

Google does support NodeJS, but on App Engine Flexible Enviroment, not in Standard environment , and the article you shared is using Standard environment. That article is also using a specific get_serving_url() method in Python which is not implemented in NodeJS .

However I think you can achieve some similar functionality by using Signed URLs . You have an example here on how to get Signed URLs in NodeJS. As you can see you can get write permissions that would overwrite the file any time you use the SignedURL:

//- // Generate a URL to allow write permissions. This means anyone with this URL // can send a POST request with new data that will overwrite the file. //-

You could use the code in the example:

file.getSignedUrl({
  action: 'write',
  expires: '03-17-2025'
}, function(err, url) {
  if (err) {
    console.error(err);
    return;
  }

  // The file is now available to be written to.
  var writeStream = request.put(url);
  writeStream.end('New data');

  writeStream.on('complete', function(resp) {
    // Confirm the new content was saved.
    file.download(function(err, fileContents) {
      console.log('Contents:', fileContents.toString());
      // Contents: New data
    });
  });
}); 

After you get the signed URL for the image, you can use that same URL every time you want to upload the same image resized. Only that you would have to find the way to resize the image on your own.

There is also this nodejs package for managing the signed URLs.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM