简体   繁体   English

通过http下载并使用Node.js流传输到Google Cloud存储

[英]Download via http and streaming upload to Google Cloud storage using nodejs

I am fighting to get a download to directly stream to GCS without saving to the file system, see below snippet. 我正在争取获得一个直接下载到GCS而不保存到文件系统的下载,请参见下面的代码片段。

const { Storage } = require('@google-cloud/storage');
const http = require('http');
const storage = new Storage();
const fs = require("fs");
const bucketName = 'BUCKETNAMEHERE';
const blobName = 'image.jpg';
const bucket = storage.bucket(bucketName);
const blob = bucket.file(blobName);

const streamDownload = () => {
    http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg")
        .pipe(blob.createWriteStream({
            metadata: {
                contentType: 'image/jpg'
            }
        }))
        .on("error", (err) => {
            console.error(`error occurred`);
        })
        .on('finish', () => {
            console.info(`success`);
        });
};

On finish is never triggered. 完成时永远不会触发。 No output whatsoever. 没有任何输出。 I can stream http.get to a local file without problems, so that part seems to be OK. 我可以将http.get流式传输到本地文件而不会出现问题,因此该部分似乎还可以。

What also works is streaming from the local filesystem to GCS, like below : 也可以从本地文件系统流到GCS,如下所示:

const streamFs = () => {
    fs.createReadStream('/path/to/mqdefault.jpg')
        .pipe(blob.createWriteStream({
            metadata: {
                contentType: 'image/jpg'
            }
        }))
        .on("error", (err) => {
            console.error(`error occurred`);
        })
        .on('finish', () => {
            console.info(`success`);
        });
};

The second snippet logs 'success' and the file is present on the bucket. 第二个片段记录为“成功”,并且文件存在于存储桶中。

Both http.get and fs.createReadStream create a read stream. http.getfs.createReadStream创建读取流。

What am I doing wrong here ? 我在这里做错了什么?

Switching over to the request library does work : 切换到request库确实可以:

const request = require('request');
const streamDownload = () => {
    request.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg")
        .pipe(blob.createWriteStream({
            metadata: {
                contentType: 'image/jpg'
            }
        }))
        .on("error", (err) => {
            console.error(`error occurred`);
        })
        .on('finish', () => {
            console.info(`success`);
        });
};

Still unsure why http library does not. 仍然不确定为什么http库没有。

the request library works with promises whereas the http lib needs a callback : 请求库可以使用promise,而http lib需要回调:

const streamDownload = () => {

  var stream = blob.createWriteStream({
    metadata: {
        contentType: 'image/jpg'
    }
  })

  http.get("http://i3.ytimg.com/vi/J---aiyznGQ/mqdefault.jpg", function(res){
        stream.on("error", (err) => {
            console.error(`error occurred`);
        })
        stream.on('finish', () => {
            console.info(`success`);
        });
        res.pipe(stream)            
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用nodejs将内存文件数据上传到谷歌云存储? - How to upload an in memory file data to google cloud storage using nodejs? 使用 NodeJS 将文件上传到谷歌云存储 - Upload file to google cloud storage with NodeJS 谷歌云存储多部分图像上传(NodeJS) - Google Cloud Storage Multipart Image upload (NodeJS) 使用 NodeJS 从 Google Cloud Storage 中的一组路径中检索和下载所有文件 - Retrieve and download all files from a set of paths in Google Cloud Storage using NodeJS 使用Node.js存档器下载多个Google云存储文件并将其压缩 - Using nodejs archiver to download multiple google cloud storage files and zip them 将文件从谷歌云存储直接下载到nodejs中的客户端 - Download file from google cloud storage directly to client in nodejs 如何使用 nodeJS 将 CSV 文件上传到 Google Cloud Storage - How do i upload an CSV file to Google Cloud Storage, using nodeJS 无法从Node.js上传到Google Cloud或Firebase存储桶 - Can't upload to google cloud or firebase storage bucket from nodejs 如何在 Google Cloud Function 的 /tmp 文件夹中下载文件,然后将其上传到 Google Cloud Storage - How to download files in /tmp folder of Google Cloud Function and then upload it in Google Cloud Storage 使用 Google App Engine 将大文件上传到 Google Cloud Storage - Upload large files to Google Cloud Storage using Google App Engine
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM