gpt4 book ai didi

javascript - 如何使用 for/loop (forEach) 将多个文件上传到 Google Drive API

转载 作者:太空宇宙 更新时间:2023-11-04 01:35:56 27 4
gpt4 key购买 nike

我使用 Google Drive API 上传多个文件。我在上传多个文件时遇到 RAM 不足的问题。我尝试在我的代码中使用 forEach (for 循环)来避免同时上传多个文件,但它没有按我预期的方式工作。它总是循环遍历整个列表文件并同时上传。

我尝试使用 async/await 语法来阻止循环,但它没有按我预期的方式工作。这是我的代码:

const fs = require("fs");
const readline = require("readline");
const { google } = require("googleapis");

let files = ["file1.mp4", "file2.mp4"];

const SCOPES = ["https://www.googleapis.com/auth/drive.metadata.readonly"];

const TOKEN_PATH = "token.json";
fs.readFile("credentials.json", (err, content) => {
if (err) return console.log("Error loading client secret file:", err);
// Authorize a client with credentials, then call the Google Drive API.
authorize(JSON.parse(content), uploadFiles);
});

function authorize(credentials, callback) {
const { client_secret, client_id, redirect_uris } = credentials.installed;
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);

// Check if we have previously stored a token.
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getAccessToken(oAuth2Client, callback);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client);
});
}

function getAccessToken(oAuth2Client, callback) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: "offline",
scope: SCOPES
});
console.log("Authorize this app by visiting this url:", authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question("Enter the code from that page here: ", code => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error("Error retrieving access token", err);
oAuth2Client.setCredentials(token);
// Store the token to disk for later program executions
fs.writeFile(TOKEN_PATH, JSON.stringify(token), err => {
if (err) console.error(err);
console.log("Token stored to", TOKEN_PATH);
});
callback(oAuth2Client);
});
});
}

async function uploadFiles(auth) {
for (file of files) {
var fileMetadata = {
name: file
};
var media = {
body: fs.createReadStream("test/" + file)
};
google.drive({ version: "v3", auth });
const result = await drive.files.create(
{
resource: fileMetadata,
media: media,
fields: "id"
},
function(err, fileid) {
if (err) {
// Handle error
console.error(err);
} else {
console.log("File Id: ", fileid.data.id);
console.log("Uploaded..:" + file);
}
}
);
console.log("Uploading file..:" + file);
}
}

我只是想问为什么循环不上传单个文件?

最佳答案

I try to use forEach (for loop) for my code to avoid uploading multiple files at the same time

不能,该过程完全是异步的。您将回调作为参数传递给函数 drive.files.create

顺便说一句,如果你想使用async/await,你应该将你的函数包装成一个promisified函数。

function myCreateFunc (fileInfos) {
return new Promise((resolve, reject) => {
google.drive.create(filesInfos, function callback(err, fileId) {
if(err)
reject(err)

resolve(fileId)
})
});
}

关于javascript - 如何使用 for/loop (forEach) 将多个文件上传到 Google Drive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54624397/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com