从 drive.files.get 获取可读流
Get readable stream from drive.files.get
我正在编写一个 nodejs 函数来将文件从 google 驱动器传输到其他地方
以下是从驱动器获取文件的示例代码块
var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
var dest = fs.createWriteStream('/tmp/photo.jpg');
drive.files.get({
fileId: fileId,
alt: 'media'
})
.on('end', function () {
console.log('Done');
})
.on('error', function (err) {
console.log('Error during download', err);
})
.pipe(dest);
我不想将其通过管道传输到可写流,而是希望获得一个可读流,然后将其传递给我的函数
uploadsomeplace(readableStream)
Update 1
我编写了一个函数来获取该可读流并将其再次上传到 google 驱动器中的某个文件夹(看起来很笨但是为了测试 drive.files.creates 需要一个可读流)
const transferit = async () => {
var fileId = '0BwwA4oUTe1UVNwOHItT0xfa2M' // some valid id
let somereadstream = drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
)
var folderId = '1oC90HZDLbfYiW2neSCMERHZ8ZH1X' // some valid id
var fileMetadata = {
name: 'photo.jpg',
parents: [folderId]
}
var media = {
mimeType: 'image/jpeg',
// body: fs.createReadStream('image.jpg')
body: somereadstream
}
return await drive.files.create({
resource: fileMetadata,
media: media
// fields: 'id'
})
}
transferit().then(data => console.log(data))
但我收到错误
(node:6200) UnhandledPromiseRejectionWarning: TypeError: part.body.pipe is not a function
从表面上看
drive.files.get({
fileId: fileId,
alt: 'media'
})
已经returns一个可读流。只需将它分配给一个变量并将其传递给您的函数即可。
const readableStream = drive.files.get({
fileId: fileId,
alt: 'media'
});
uploadsomeplace(readableStream);
drive.files.get returns 一个承诺,如果它得到满足,就会给出一个响应对象,其中包含 response.data
中的可读流
const isStream = require('is-stream')
const transferit = async () => {
var fileId = '1uw5PhBF8z8uBsxmFKJef582Pp92UKpxM' // some valid id
let somereadstream = await drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
)
console.log(somereadstream)
return isStream.readable(somereadstream.data)
}
这在打字稿中对我有用:
async function getReadableStreamForFileId(
drive: drive_v3.Drive,
fileId: FileId,
): Promise<stream.Readable> {
console.log('getReadableStreamForFileId', {drive, fileId});
// https://developers.google.com/drive/api/v3/manage-downloads#node.js
return (await drive.files.get(
{fileId, alt: 'media'},
{responseType: 'stream'},
)).data as any as Promise<stream.Readable>;
}
示例:
const fileId = '1F2*******************xGC';
const fileDataStream = await getReadableStreamForFileId(drive, fileId);
const target = fs.createWriteStream('data-out');
fileDataStream.pipe(target);
drive
按照 https://developers.google.com/drive/api/v3/quickstart/nodejs
中的描述进行初始化
我正在编写一个 nodejs 函数来将文件从 google 驱动器传输到其他地方
以下是从驱动器获取文件的示例代码块
var fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
var dest = fs.createWriteStream('/tmp/photo.jpg');
drive.files.get({
fileId: fileId,
alt: 'media'
})
.on('end', function () {
console.log('Done');
})
.on('error', function (err) {
console.log('Error during download', err);
})
.pipe(dest);
我不想将其通过管道传输到可写流,而是希望获得一个可读流,然后将其传递给我的函数
uploadsomeplace(readableStream)
Update 1
我编写了一个函数来获取该可读流并将其再次上传到 google 驱动器中的某个文件夹(看起来很笨但是为了测试 drive.files.creates 需要一个可读流)
const transferit = async () => {
var fileId = '0BwwA4oUTe1UVNwOHItT0xfa2M' // some valid id
let somereadstream = drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
)
var folderId = '1oC90HZDLbfYiW2neSCMERHZ8ZH1X' // some valid id
var fileMetadata = {
name: 'photo.jpg',
parents: [folderId]
}
var media = {
mimeType: 'image/jpeg',
// body: fs.createReadStream('image.jpg')
body: somereadstream
}
return await drive.files.create({
resource: fileMetadata,
media: media
// fields: 'id'
})
}
transferit().then(data => console.log(data))
但我收到错误
(node:6200) UnhandledPromiseRejectionWarning: TypeError: part.body.pipe is not a function
从表面上看
drive.files.get({
fileId: fileId,
alt: 'media'
})
已经returns一个可读流。只需将它分配给一个变量并将其传递给您的函数即可。
const readableStream = drive.files.get({
fileId: fileId,
alt: 'media'
});
uploadsomeplace(readableStream);
drive.files.get returns 一个承诺,如果它得到满足,就会给出一个响应对象,其中包含 response.data
中的可读流const isStream = require('is-stream')
const transferit = async () => {
var fileId = '1uw5PhBF8z8uBsxmFKJef582Pp92UKpxM' // some valid id
let somereadstream = await drive.files.get(
{ fileId, alt: 'media' },
{ responseType: 'stream' }
)
console.log(somereadstream)
return isStream.readable(somereadstream.data)
}
这在打字稿中对我有用:
async function getReadableStreamForFileId(
drive: drive_v3.Drive,
fileId: FileId,
): Promise<stream.Readable> {
console.log('getReadableStreamForFileId', {drive, fileId});
// https://developers.google.com/drive/api/v3/manage-downloads#node.js
return (await drive.files.get(
{fileId, alt: 'media'},
{responseType: 'stream'},
)).data as any as Promise<stream.Readable>;
}
示例:
const fileId = '1F2*******************xGC';
const fileDataStream = await getReadableStreamForFileId(drive, fileId);
const target = fs.createWriteStream('data-out');
fileDataStream.pipe(target);
drive
按照 https://developers.google.com/drive/api/v3/quickstart/nodejs