如何createWriteStream()到GCS? [英] how to createWriteStream() to GCS?

查看:36
本文介绍了如何createWriteStream()到GCS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个Express路由,该路由在POST正文中使用一个图像URI,然后将图像保存到Google Cloud Storage Bucket中.

I'm trying to write an Express route that takes an image URI in the POST body and then saves the image into a Google Cloud Storage Bucket.

我无法将该映像持久保存到本地磁盘,需要将缓冲区直接流式传输到GCS存储桶.

I'm not able to persist this image to local disk and need to stream the buffer straight to the GCS bucket.

我的路线创建了一个4KB的存根",在GCS存储桶中,但是没有图像有效载荷.然后我的nodejs崩溃了……

My route creates a 4KB "stub" in the GCS bucket but there's no image payload. My nodejs then proceeds to crash...

问:将https.request()的结果.pipe()到blob.createWriteStream()的正确方法是什么?这是正确的方法吗?

我花了几天的时间来尝试使用不同的流处理程序来找到正确的解决方案,但是几乎没有什么进展可显示.有人可以帮忙吗?

I've spent a few days trying to find the right solution with different stream handlers but with precious little progress to show for it. Can someone help?

message: 'The rate of change requests to the object my-projectID/testimage.jpg exceeds the rate limit. Please reduce the rate of create, update, and delete requests.'

  const streamifier = require('streamifier');
  const {Storage} = require('@google-cloud/storage');
  const storage = new Storage({
      projectId: 'my-projectID',
      keyFile: '../config/my-projectID.json'
      });
  const bucket = storage.bucket('my-projectID');
  const blob = bucket.file('testimg.jpg');

app.post('/bam', passport.authenticate('basic', {session: false }), (req, res) => {
    
return new Promise((resolve, reject) => {
        https.request(req.body.pic, (response) => {            
            response.on('data', (d) => {
                streamifier.createReadStream(d)
                .pipe(blob.createWriteStream({
                    resumable:false, 
                    public:true,
                    metadata:{ contentType: 'image/jpeg' }
                    }));
            });
            
            response.on('finish', (done) => {
                console.log(done);
            });

            response.on('error', (err) => {
                console.log(err);
            });

        }).end();
    }); 

});

**为我的丑陋JS致歉,我仍然处于ES6学习曲线的底部.

** Apologies for my ugly JS, I'm still at the bottom of ES6 learning curve.

推荐答案

我没有尝试使用https.request(),但是我可以直接将图像上传到GCS,而无需按照云存储Node.js客户端:

I haven't tried with https.request(), but I was able to upload an image directly to GCS without storing it locally by following the Cloud Storage Node.js Client:

const fs = require('fs')
const request = require('request')
const {Storage} = require('@google-cloud/storage');
const storage = new Storage();
const myBucket = storage.bucket('my-bucket');
const blob = myBucket.file('image.jpg');
 
const uploadblob = (url, callback) => {
  request.head(url, (err, res, body) => {
    request(url)
      .pipe(blob.createWriteStream())
      .on('close', callback)
  })
}
const url = 'https://www.example/image.jpg'
 
uploadblob(url, () => {console.log('Done!')})

这篇关于如何createWriteStream()到GCS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆