如何从Node中的图片网址将图片上传到Google云端存储?

时间:2021-03-24 17:13:04

Given an image url, how can I upload that image to Google Cloud Storage for image processing using Node.js?

给定图像网址,如何使用Node.js将该图像上传到Google云端存储以进行图像处理?

3 个解决方案

#1


13  

It's a 2 steps process:

这是一个两步的过程:

  • Download file locally using request or fetch.
  • 使用请求或提取在本地下载文件。

  • Upload to GCL with the official library.

    使用官方库上传到GCL。

    var fs = require('fs');
    var gcloud = require('gcloud');
    
    // Authenticating on a per-API-basis. You don't need to do this if you auth on a
    // global basis (see Authentication section above).
    
    var gcs = gcloud.storage({
      projectId: 'my-project',
      keyFilename: '/path/to/keyfile.json'
    });
    
    // Create a new bucket.
    gcs.createBucket('my-new-bucket', function(err, bucket) {
      if (!err) {
        // "my-new-bucket" was successfully created.
      }
    });
    
    // Reference an existing bucket.
    var bucket = gcs.bucket('my-existing-bucket');                
    var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
    var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
    localReadStream.pipe(remoteWriteStream)
      .on('error', function(err) {})
      .on('finish', function() {
        // The file upload is complete.
      });
    

If you would like to save the file as a jpeg image, you will need to edit the remoteWriteStream stream and add custom metadata:

如果要将文件另存为jpeg图像,则需要编辑remoteWriteStream流并添加自定义元数据:

var image = bucket.file('zebra.jpg');
localReadStream.pipe(image.createWriteStream({
    metadata: {
      contentType: 'image/jpeg',
      metadata: {
        custom: 'metadata'
      }
    }
}))

I found this while digging through this documentation

我在挖掘这个文档时发现了这一点

#2


2  

To add onto Yevgen Safronov's answer, we can pipe the request into the write stream without explicitly downloading the image into the local file system.

为了添加Yevgen Safronov的答案,我们可以将请求传递到写入流中,而无需将图像显式下载到本地文件系统中。

const request = require('request');
const storage = require('@google-cloud/storage')();

function saveToStorage(attachmentUrl, bucketName, objectName) {
  const req = request(attachmentUrl);
  req.pause();
  req.on('response', res => {

    // Don't set up the pipe to the write stream unless the status is ok.
    // See https://*.com/a/26163128/2669960 for details.
    if (res.statusCode !== 200) {
      return;
    }

    const writeStream = storage.bucket(bucketName).file(objectName)
      .createWriteStream({

        // Tweak the config options as desired.
        gzip: true,
        public: true,
        metadata: {
          contentType: res.headers['content-type']
        }
      });
    req.pipe(writeStream)
      .on('finish', () => console.log('saved'))
      .on('error', err => {
        writeStream.end();
        console.error(err);
      });

    // Resume only when the pipe is set up.
    req.resume();
  });
  req.on('error', err => console.error(err));
}

#3


0  

Incase of handling image uploads from a remote url. In reference to the latest library provided by Google docs. Instead of storing the buffer of image. We can directly send it to storage.

从远程网址处理图像上传。参考Google文档提供的最新库。而不是存储图像的缓冲区。我们可以直接将其发送到存储。

function sendUploadUrlToGCS(req, res, next) {
  if (!req.body.url) {
    return next();
  }

  var gcsname = Date.now() + '_name.jpg';
  var file = bucket.file(gcsname);

  return request({url: <remote-image-url>, encoding: null}, function(err, response, buffer) {
    req.file = {};
    var stream = file.createWriteStream({
      metadata: {
        contentType: response.headers['content-type']
      }
    });

    stream.on('error', function(err) {
       req.file.cloudStorageError = err;
       console.log(err);
       next(err);
    });

    stream.on('finish', function() {
      req.file.cloudStorageObject = gcsname;
      req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
      next();
    });

    stream.end(buffer);
  });
}

#1


13  

It's a 2 steps process:

这是一个两步的过程:

  • Download file locally using request or fetch.
  • 使用请求或提取在本地下载文件。

  • Upload to GCL with the official library.

    使用官方库上传到GCL。

    var fs = require('fs');
    var gcloud = require('gcloud');
    
    // Authenticating on a per-API-basis. You don't need to do this if you auth on a
    // global basis (see Authentication section above).
    
    var gcs = gcloud.storage({
      projectId: 'my-project',
      keyFilename: '/path/to/keyfile.json'
    });
    
    // Create a new bucket.
    gcs.createBucket('my-new-bucket', function(err, bucket) {
      if (!err) {
        // "my-new-bucket" was successfully created.
      }
    });
    
    // Reference an existing bucket.
    var bucket = gcs.bucket('my-existing-bucket');                
    var localReadStream = fs.createReadStream('/photos/zoo/zebra.jpg');
    var remoteWriteStream = bucket.file('zebra.jpg').createWriteStream();
    localReadStream.pipe(remoteWriteStream)
      .on('error', function(err) {})
      .on('finish', function() {
        // The file upload is complete.
      });
    

If you would like to save the file as a jpeg image, you will need to edit the remoteWriteStream stream and add custom metadata:

如果要将文件另存为jpeg图像,则需要编辑remoteWriteStream流并添加自定义元数据:

var image = bucket.file('zebra.jpg');
localReadStream.pipe(image.createWriteStream({
    metadata: {
      contentType: 'image/jpeg',
      metadata: {
        custom: 'metadata'
      }
    }
}))

I found this while digging through this documentation

我在挖掘这个文档时发现了这一点

#2


2  

To add onto Yevgen Safronov's answer, we can pipe the request into the write stream without explicitly downloading the image into the local file system.

为了添加Yevgen Safronov的答案,我们可以将请求传递到写入流中,而无需将图像显式下载到本地文件系统中。

const request = require('request');
const storage = require('@google-cloud/storage')();

function saveToStorage(attachmentUrl, bucketName, objectName) {
  const req = request(attachmentUrl);
  req.pause();
  req.on('response', res => {

    // Don't set up the pipe to the write stream unless the status is ok.
    // See https://*.com/a/26163128/2669960 for details.
    if (res.statusCode !== 200) {
      return;
    }

    const writeStream = storage.bucket(bucketName).file(objectName)
      .createWriteStream({

        // Tweak the config options as desired.
        gzip: true,
        public: true,
        metadata: {
          contentType: res.headers['content-type']
        }
      });
    req.pipe(writeStream)
      .on('finish', () => console.log('saved'))
      .on('error', err => {
        writeStream.end();
        console.error(err);
      });

    // Resume only when the pipe is set up.
    req.resume();
  });
  req.on('error', err => console.error(err));
}

#3


0  

Incase of handling image uploads from a remote url. In reference to the latest library provided by Google docs. Instead of storing the buffer of image. We can directly send it to storage.

从远程网址处理图像上传。参考Google文档提供的最新库。而不是存储图像的缓冲区。我们可以直接将其发送到存储。

function sendUploadUrlToGCS(req, res, next) {
  if (!req.body.url) {
    return next();
  }

  var gcsname = Date.now() + '_name.jpg';
  var file = bucket.file(gcsname);

  return request({url: <remote-image-url>, encoding: null}, function(err, response, buffer) {
    req.file = {};
    var stream = file.createWriteStream({
      metadata: {
        contentType: response.headers['content-type']
      }
    });

    stream.on('error', function(err) {
       req.file.cloudStorageError = err;
       console.log(err);
       next(err);
    });

    stream.on('finish', function() {
      req.file.cloudStorageObject = gcsname;
      req.file.cloudStoragePublicUrl = getPublicUrl(gcsname);
      next();
    });

    stream.end(buffer);
  });
}