簡體   English   中英

如何使用Google API Node.js客戶端更新Google雲端硬盤上的現有文件?

[英]How to update existing file on Google Drive with the Google API Node.js client?

我使用Google API Node.js客戶端通過Node.js程序管理Google雲端硬盤上的文件。

我已成功使用此庫下載了文件,現在我想將文件的更新版本上傳回Google雲端硬盤。

文檔很好地解釋了如何在Google雲端硬盤上創建文件:

var fileMetadata = {
  'name': 'photo.jpg'
};
var media = {
  mimeType: 'image/jpeg',
  body: fs.createReadStream('files/photo.jpg')
};
drive.files.create({
  resource: fileMetadata,
  media: media,
  fields: 'id'
}, function (err, file) {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id: ', file.id);
  }
});

但是,我找不到任何有關如何用新版本覆蓋現有文件的信息。

與此類似的 Stack Overflow上的其他帖子以及Google文檔 ,僅說明了如何直接使用Rest API更新文件。

我想知道是否有一種更簡單的方法–因為提供了一個不錯的API客戶端庫,可以讓我創建新文件,所以當然也應該讓我更新現有文件嗎?

要更新現有文件,請使用drive.files.update在此處定義)。

drive.files.update({
  fileId: file.id,
  resource: fileMetadata,
  media: media
}, (err, file) => {
  if (err) {
    // Handle error
    console.error(err);
  } else {
    console.log('File Id: ', file.id);
  }
});

請注意,對於drive.files.createdrive.files.update ,也可以使用promise接口和async / await

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM