I'm trying to upload a file to Google Drive. After I run the program, I get status 200 but I can't find the file in Drive. I entered a folder ID in the fileMetadata
variable under the parents
key.
This is my code:
const { google } = require('googleapis');
const credentials = require('./credentials.json');
const fs = require('fs')
const scopes = [
'https://www.googleapis.com/auth/drive',
'https://www.googleapis.com/auth/drive.file',
'https://www.googleapis.com/auth/drive.appdata',
];
const auth = new google.auth.JWT(
credentials.client_email, null,
credentials.private_key, scopes
);
const drive = google.drive({ version: "v3", auth });
auth.authorize(function(err, tokens) {
if (err) {
console.log(err);
return;
} else {
console.log('It authorized successfully!');
console.log(`The Token is: ${JSON.stringify(tokens)}`)
var data = fs.createReadStream('./Test.txt')
var fileMetadata = {
'name': 'photo.txt',
'parents':[{'id':'1MiFB-XXWFRHjxZwTmE9FxsCGb9Een_2Y'}] //Folder ID
};
var media = {
mimeType: 'text/plain',
body: data //
};
drive.files.create({
resource: fileMetadata,
media: media,
fields: 'id'
}).then (function(err, file) {
if(err) {
// Handle error
console.log(err);
} else {
console.log('File Id: ', file.id);
}
});
}
});
And that's the response I got back:
{
config: {
url: 'https://www.googleapis.com/upload/drive/v3/files?fields=id&uploadType=multipart',
method: 'POST',
paramsSerializer: [Function (anonymous)],
data: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_flush: [Function: flush],
[Symbol(kCapture)]: false,
[Symbol(kTransformState)]: [Object]
},
headers: {
'Content-Type': 'multipart/related; boundary=7b25465c-4ee3-450d-b41c-7efd51e4637d',
'Accept-Encoding': 'gzip',
'User-Agent': 'google-api-nodejs-client/0.7.2 (gzip)',
Authorization: 'Bearer `TOKEN`,
Accept: 'application/json'
},
params: [Object: null prototype] { fields: 'id', uploadType: 'multipart' },
validateStatus: [Function (anonymous)],
body: PassThrough {
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 2,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
_flush: [Function: flush],
[Symbol(kCapture)]: false,
[Symbol(kTransformState)]: [Object]
},
responseType: 'json'
},
data: { id: '1up-LCc9Fc0D2_t7OdJvF9D2QEVwxhV8y' },
headers: {
'alt-svc': 'h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"',
'cache-control': 'no-cache, no-store, max-age=0, must-revalidate',
connection: 'close',
'content-length': '47',
'content-type': 'application/json; charset=UTF-8',
date: 'Mon, 27 Jul 2020 09:54:42 GMT',
expires: 'Mon, 01 Jan 1990 00:00:00 GMT',
pragma: 'no-cache',
server: 'UploadServer',
vary: 'Origin, X-Origin',
'x-guploader-uploadid': 'AAANsUma96bf0mAHkz32b8QuarZvQBuez_rVwOmhVV-ESgd1IccLyYYnn3xdT--jVFDdo9zC9Qz_VqAVyaHK-OcQWFA'
},
status: 200,
statusText: 'OK'
}
Where is the file? What's not right here?
Thank you!!
Aucun commentaire:
Enregistrer un commentaire