- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在做一个项目,我正在阅读使用 SimpleMind 创建的思维导图文件从 Google Drive,修改文件,然后将它们上传回 Google Drive。
SimpleMind 创建的 SMMX 文件是包含 XML 文件和媒体文件的 zip 文件。
我的程序在本地运行时运行良好,我对思维导图所做的更改会显示在 SimpleMind 中。
我现在想使用 App Engine 在 Google Cloud Platform 上运行该程序。
由于安全限制,我不能只将我从 Google Drive 下载的文件写入云中的应用服务器的文件系统。相反,我创建了一个存储桶来存储文件。
但是,当我这样做时,我的文件被损坏了,在我运行我的程序之后,它不是 zip 文件的内容,而是一个 JSON 文件,显然是读取流的字符串表示。
本地运行 - 工作
这是我的代码的简化版本,没有对 zip 文件进行实际修改,我将其省略了,因为它与问题以及任何错误处理无关 - 从来没有任何错误。
当我在本地运行代码时,我使用写入流和读取流在本地文件系统上保存和加载文件:
#!/usr/bin/env node
const { readFileSync, createReadStream, createWriteStream } = require('fs');
const { google } = require('googleapis');
const tokenPath = 'google-drive-token.json';
const clientId = 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
const redirectUri = 'urn:ietf:wg:oauth:2.0:oob';
const clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const fileId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const fileName = 'deleteme.smmx';
(async () => {
const auth = new google.auth.OAuth2(clientId, clientSecret, redirectUri);
const token = JSON.parse(readFileSync(tokenPath));
auth.setCredentials(token);
const writeStream = createWriteStream(fileName);
const drive = google.drive({ version: 'v3', auth });
let progress = 0;
const res = await drive.files.get({ fileId, alt: 'media' }, { responseType: 'stream' });
await new Promise(resolve => {
res.data.on('data', d => (progress += d.length)).pipe(writeStream);
writeStream.on('finish', () => {
console.log(`Done downloading file ${fileName} from Google Drive to local file system (${progress} bytes)`);
resolve();
});
});
const readStream = createReadStream(fileName);
progress = 0;
const media = {
mimeType: 'application/x-zip',
body: readStream
.on('data', d => {
progress += d.length;
})
.on('end', () => console.log(`${progress} bytes read from local file system`))
};
await drive.files.update({
fileId,
media
});
console.log(`File ${fileName} successfully uploaded to Google Drive`);
})();
Done downloading file deleteme.smmx from Google Drive to local file system (371 bytes)
371 bytes read from local file system
File deleteme.smmx successfully uploaded to Google Drive
const { readFileSync } = require('fs');
const { google } = require('googleapis');
const { Storage } = require('@google-cloud/storage');
const tokenPath = 'google-drive-token.json';
const clientId = 'xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
const redirectUri = 'urn:ietf:wg:oauth:2.0:oob';
const clientSecret = 'xxxxxxxxxxxxxxxxxxxxxxxx';
const fileId = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
const fileName = 'deleteme.smmx';
const storageBucketId = 'xxxxxxxxxxx';
module.exports = async () => {
const auth = new google.auth.OAuth2(clientId, clientSecret, redirectUri);
const token = JSON.parse(readFileSync(tokenPath));
auth.setCredentials(token);
const storage = new Storage();
const bucket = storage.bucket(storageBucketId);
const file = bucket.file(fileName);
const writeStream = file.createWriteStream({ resumable: false });
const drive = google.drive({ version: 'v3', auth });
let progress = 0;
const res = await drive.files.get({ fileId, alt: 'media' }, { responseType: 'stream' });
await new Promise(resolve => {
res.data.on('data', d => (progress += d.length)).pipe(writeStream);
writeStream.on('finish', () => {
console.log(`Done downloading file ${fileName} from Google Drive to Cloud bucket (${progress} bytes)`);
resolve();
});
});
const readStream = file.createReadStream();
progress = 0;
const media = {
mimeType: 'application/x-zip',
body: readStream
.on('data', d => {
progress += d.length;
})
.on('end', () => console.log(`${progress} bytes read from storage`))
};
await drive.files.update({
fileId,
media
});
console.log(`File ${fileName} successfully uploaded to Google Drive`);
return 0;
};
fs
模块,我用的是对应的方法
file.createWriteStream和
file.createReadStream来自谷歌云存储库。
Done downloading file deleteme.smmx from Google Drive to Cloud bucket (371 bytes)
371 bytes read from storage
File deleteme.smmx successfully uploaded to Google Drive
{
"_readableState": {
"objectMode": false,
"highWaterMark": 16384,
"buffer": { "head": null, "tail": null, "length": 0 },
"length": 0,
"pipes": null,
"pipesCount": 0,
"flowing": true,
"ended": false,
"endEmitted": false,
"reading": false,
"sync": false,
"needReadable": true,
"emittedReadable": false,
"readableListening": false,
"resumeScheduled": true,
"paused": false,
"emitClose": true,
"destroyed": false,
"defaultEncoding": "utf8",
"awaitDrain": 0,
"readingMore": false,
"decoder": null,
"encoding": null
},
"readable": true,
"_events": {},
"_eventsCount": 4,
"_writableState": {
"objectMode": false,
"highWaterMark": 16384,
"finalCalled": false,
"needDrain": false,
"ending": false,
"ended": false,
"finished": false,
"destroyed": false,
"decodeStrings": true,
"defaultEncoding": "utf8",
"length": 0,
"writing": false,
"corked": 0,
"sync": true,
"bufferProcessing": false,
"writecb": null,
"writelen": 0,
"bufferedRequest": null,
"lastBufferedRequest": null,
"pendingcb": 0,
"prefinished": false,
"errorEmitted": false,
"emitClose": true,
"bufferedRequestCount": 0,
"corkedRequestsFree": { "next": null, "entry": null }
},
"writable": true,
"allowHalfOpen": true,
"_transformState": {
"needTransform": false,
"transforming": false,
"writecb": null,
"writechunk": null,
"writeencoding": null
},
"_destroyed": false
}
drive.files.update
方法最佳答案
显然,您可以使用直通流
const file = storage.bucket(bucketName).file(object.name)
const fileStream = file.createReadStream();
const dataStream = new stream.PassThrough();
fileStream.pipe(dataStream);
await uploadFileToGDrive(dataStream, {
name: object.name,
mimeType: object.contentType,
parents: ['shared_dir_in_g_drive'],
})
关于node.js - 无法将文件读取流从 Google Cloud Storage 传输到 Google Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57151911/
嗨,当尝试将图像上传到 firebase 存储时,我正在使用 firebase 文档,但是出现此错误。 在范围内找不到“存储” let storage = Storage.storage() le
我最近在使用 Firebase 存储时遇到了一些问题。 当我们尝试访问刚刚上传的文件时,浏览器中出现此错误消息 { "error": { "code": 400,
这些是在不同版本的 NuGet 包之间迁移的重要指南: https://github.com/Azure/azure-sdk-for-net/blob/Azure.Storage.Blobs_12.6
警告: Warning: Can't resolve all parameters for Storage in /Users/zzm/Desktop/minan/node_modules/@ioni
我在圆形立方体中收到此错误(“连接到存储服务器失败”)行。我已经检查了所有内容,配置和数据库用户名密码,服务器详细信息都是干净的。谁能告诉我可能是什么问题。这里我给出了整个配置文件。
我希望能够限制容器的大小,但是使用默认的存储驱动程序aufs(对于Ubuntu 14.04),当我尝试使用--storage-opt参数时出现错误 $ docker create -it --name
我希望能够支持对使用 Google Cloud Storage 托管的静态 Assets 进行 Brotli 和 Gzip 编码。为此,我想在将文件上传为 之前对其进行编码, .gz和 .br .问
场景 我有几个由 Google Cloud Storage object.finalize 事件触发的 Google Cloud Functions。为此,我使用两个存储桶并使用“同步选项:覆盖目标位
我在 Google Cloud Storage 中有一个存储桶和一个网站。人们目前可以通过网站上传到存储桶(使用 Google 身份验证)。 但是,我需要设置它以便任何人都可以查看上传的文件(并且不能
如果文件被放入 Google Cloud 存储并公开,但该文件的网址在另一个网页上不存在,那么 Google 是否会在其搜索结果中将其编入索引?有人知道吗? 最佳答案 Google 的搜索索引独立于其
截至今天早上,我无法访问我的存储桶。 当我在导航上选择 Google Cloud Storage 选项卡时,一切都按预期加载,但不是显示我的两个存储桶,而是显示一个警告栏说: We were unab
我想弄清楚是否可以在 Windows 平台上使用 gsutil 的 cp 命令将文件上传到 Google Cloud Storage。我的本地计算机上有 6 个文件夹,每天都会向其中添加新的 pdf
我最近开始使用 Google Cloud Storage。最初我在安装 Cloud SDK 时创建了一个虚拟项目。现在我正在做另一个项目。 gsutil 仍然指向我以前的项目。我如何使它指向我的新项目
我目前正在这样做,但它非常慢,因为我的存储桶中有几 TB 的数据: gsutil du -sh gs://my-bucket-1/ 对于子文件夹也是如此: gsutil du -sh gs://my-
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
将文件上传到 Google Cloud Storage 时,有一个自定义数据字段元数据。 Google's example相当短: var metadata = { contentType: 'a
这可能看起来很天真,我知道我们可以在 blob 中创建文件夹,并且这些文件夹仍然存储在容器中。我们仍然可以对这些“blob 中包含的文件夹”执行通常对文件存储中的文件夹执行的所有操作。 我们仍然可以像
我有一个包含超过 2 万个文件名的 Google Storage 存储桶。有没有办法在短时间内列出存储桶中的所有文件名? 最佳答案 这取决于您所说的“短”是什么意思,但是: 您可以做的一件事来加快列出
有谁知道如果文件不存在,您是否需要为 Google Cloud Storage 中的文件请求付费?换句话说,有人访问您存储桶中不存在的文件是否计入您的请求?还是仅适用于存在的文件? 最佳答案 客户无需
在每一分钟结束时,我的代码总共会上传 20 到 40 个文件(从多台机器上并行上传大约 5 个文件,直到全部上传完毕)到 Google Cloud Storage。我经常收到 429 - Too Ma
我是一名优秀的程序员,十分优秀!