javascript - 如何使用 Drive API 设置文件的元数据?
问题描述
我正在寻找一种解决方案来上传带有一些特定元数据的文件!我尝试将canDownload设置为false,但结果没有反映!我想将文件设置为每个人都可以访问。这是我的代码:
const jwToken = new google.auth.JWT(
token.client_email,
null,
token.private_key,
["https://www.googleapis.com/auth/drive"],
null
);
jwToken.authorize((err : any) => {
if (err) {
console.log(err);
} else {
console.log("Authentication Success!");
}
});
const metaData = {
'name': filename,
"shared": true,
"capabilities" : {
"canDownload": false
},
parents: [folderId]
};
const media = {
mimeType: 'Application/Pdf',
body: fileSystem.createReadStream(pathSystem.join(path))
};
drive.files.create({
auth : jwToken,
resource : metaData,
media : media,
fields : 'id'
}, (err : any, file : any) => {
if (err) {
console.log("coming!!");
throw err;
} else {
console.log("File Upload Successfully!");
}
});
解决方案
- 您不想让用户下载创建的文件。
- 您想与任何读者分享。
- 您想使用 googleapis 和 Node.js 来实现这一点。
- 您的脚本是打字稿。然后你构建它并使用 Node.js 运行它。
- 您已经能够使用 Drive API 将文件创建到 Google Drive。
如果我的理解是正确的,这个答案怎么样?请认为这只是几个可能的答案之一。
修改点:
关于capabilities
:
不幸的是, 的属性capabilities.canDownload
不是writable
。这样,作为一种方法,如何使用copyRequiresWriterPermission
?使用它,该文件被设置为“禁用选项以供评论者和查看者下载、打印和复制”。
关于shared
:
的属性shared
也不是writable
。因此,当您想与任何读者分享时,请使用 Permissions: create in Drive API 的方法。
修改后的脚本:
当您的脚本被修改时,请进行如下修改。
从:
const metaData = {
'name': filename,
"shared": true,
"capabilities" : {
"canDownload": false
},
parents: [folderId]
};
const media = {
mimeType: 'Application/Pdf',
body: fileSystem.createReadStream(pathSystem.join(path))
};
drive.files.create({
auth : jwToken,
resource : metaData,
media : media,
fields : 'id'
}, (err : any, file : any) => {
if (err) {
console.log("coming!!");
throw err;
} else {
console.log("File Upload Successfully!");
}
});
至:
const metaData = {
name: filename,
parents: [folderId],
copyRequiresWriterPermission: true
};
const media = {
mimeType: "application/pdf",
body: fileSystem.createReadStream(pathSystem.join(path))
};
drive.files.create(
{
auth: jwToken,
resource: metaData,
media: media,
fields: "id"
},
(err: any, file: any) => {
if (err) {
console.log(err);
return;
}
const fileId = file.data.id;
console.log(fileId);
const body = {
auth: jwToken,
fileId: fileId,
requestBody: {
role: "reader",
type: "anyone"
}
};
drive.permissions.create(body, (err: any, res: any) => {
if (err) {
console.error(err);
return;
}
console.log(res.data);
});
}
);
- 为了不让用户下载上传的文件,
copyRequiresWriterPermission: true
在 Files 的请求体中添加了:create。 - 为了与任何人共享,
{role: "reader", type: "anyone"}
用于请求正文的 Permissions:create。
笔记:
- 在您的脚本中,使用了服务帐户。因此,如果您想在 Google Drive 中查看上传的文件,请与您的 Google 帐户共享。这样,您可以使用浏览器查看它。请注意这一点。
参考:
如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
推荐阅读
- c# - 获取存储帐户中的容器列表
- python - h2o ensemble 抛出错误:“基本模型不保留交叉验证预测”
- python - 在python中以与平台无关的方式从路径中拆分根
- c++ - 将 c++ 可执行图标添加到 premake5 构建脚本
- python - 安装 python 3.7.2 后,Ubuntu 18.04 处于登录循环中
- node.js - 声明单独的 Firebase Cloud Functions 并仍然使用 Express.js
- database - 性能:在从 UNLOGGED 转换为 LOGGED 之后或之前创建索引?
- typescript - 我想在nestjs中实现自定义缓存
- swift - 如何在表的第一行填充来自查询的 MPmedia 项目之前将数据添加到表的第一行?
- mysql - 如何修复我的 FOR EACH ROW 语句未移动到下一行的错误