[英]How to set the metadata of file using Drive API?
I'm looking for a solution to uploading the files with some specific metadata!我正在寻找一种解决方案来上传具有某些特定元数据的文件! I tried to set canDownload to false, but there is no reflection in the result!
我尝试将canDownload设置为false,但结果没有反映! And I want to set the file to Everybody can access.
我想将文件设置为每个人都可以访问。 Here is my code:
这是我的代码:
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!");
}
});
If my understanding is correct, how about this answer?如果我的理解是正确的,这个答案怎么样? Please think of this as just one of several possible answers.
请将此视为几种可能的答案之一。
capabilities
:capabilities
: Unfortunately, the property of capabilities.canDownload
is not writable
.不幸的是,
capabilities.canDownload
的属性是不可writable
。 By this, as a method, how about using copyRequiresWriterPermission
?这样,作为一种方法,如何使用
copyRequiresWriterPermission
? Using this, the file is set as "Disable options to download, print and copy for commenters and viewers".使用此功能,该文件被设置为“禁用评论者和查看者的下载、打印和复制选项”。
shared
:shared
: The property of shared
is also not writable
. shared
的属性也是不可writable
。 So when you want to share with anyone as reader, please use the method of Permissions: create in Drive API.所以当你想与任何人作为读者分享时,请使用 Permissions: 在 Drive API 中创建的方法。
When your script is modified, please modify as follows.当您的脚本被修改时,请进行如下修改。
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
is added to the request body of Files: create.copyRequiresWriterPermission: true
。{role: "reader", type: "anyone"}
is used to the request body of Permissions: create.{role: "reader", type: "anyone"}
用于 Permissions: create 的请求正文。If I misunderstood your question and this was not the direction you want, I apologize.如果我误解了您的问题并且这不是您想要的方向,我深表歉意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.