简体   繁体   English

如何将文件上传到 url 中的谷歌驱动器?

[英]How can I upload files to google drive that are in a url?

I try to upload a photo that I have in a URL on another server, but it does not work for me or I do not know how to upload them in this case I am going to upload a photo but I also want to upload files that will upload to that URL.我尝试在另一台服务器上上传我在 URL 中的照片,但它对我不起作用或者我不知道如何在这种情况下上传它们我要上传照片但我也想上传文件将上传到该 URL。

const img = await fetch("http://example.com/api/photo")

await gapi.client.drive.files.create({
  resource: {
    name: "New Folder",
    body: img,
  }
})

The simple anwser is you cant do it like that.简单的答案是你不能那样做。 The file being Uploaded must be sent in the form of a stream上传的文件必须以 stream 的形式发送

Download the file to your own machine and then upload it from there.将文件下载到您自己的机器上,然后从那里上传。 Or try to figure out how to turn your url into a stream.或者尝试弄清楚如何将您的 url 变成 stream。

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);
  }
});

I believe your goal as follows.我相信你的目标如下。

  • You want to download an image data from an URL, and want to upload the downloaded image data to Google Drive.您想从 URL 下载图像数据,并将下载的图像数据上传到 Google Drive。
    • From your script, the image data is downloaded by const img = await fetch("http://example.com/api/photo") .从您的脚本中,图像数据由const img = await fetch("http://example.com/api/photo")下载。
  • You want to achieve this using googleapis for Javascript .您想使用Javascript 的 googleapis来实现这一点。

Modification points:修改点:

  • In this case, it retrieves Blob of image data from fetch , and the blob is uploaded to Google Drive.在这种情况下,它从fetch中检索 Blob 图像数据,并将该 Blob 上传到 Google Drive。
  • Unfortunately, in the current stage, it seems that although googleapis for Javascript can create new file with the metadata, the file content cannot be included.不幸的是,在当前阶段,虽然googleapis for Javascript 可以使用元数据创建新文件,但无法包含文件内容。 By this, in this answer, I use the method of this thread .通过这个,在这个答案中,我使用了这个线程的方法。 The downloaded image data is uploaded using fetch with multipart/form-data .下载的图像数据使用fetchmultipart/form-data上传。

When above poiints are reflected to your script, it becomes as follows.当以上几点反映到您的脚本时,它变成如下。

Modified script:修改后的脚本:

const img = await fetch("http://example.com/api/photo").then((e) => e.blob());

const fileMetadata = {name: "sampleName"}; // Please set filename.
const form = new FormData();
form.append('metadata', new Blob([JSON.stringify(fileMetadata)], {type: 'application/json'}));
form.append('file', img);
fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart', {
  method: 'POST',
  headers: new Headers({'Authorization': 'Bearer ' + gapi.auth.getToken().access_token}),
  body: form
}).then(res => res.json()).then(res => console.log(res));
  • By this modification, the downloaded image data is uploaded to Google Drive with multipart/form-data .通过此修改,下载的图像数据通过multipart/form-data上传到 Google Drive。

Note:笔记:

  • In this modification, it supposes as follows.在本变形例中,假设如下。
    • Your URL of http://example.com/api/photo is the direct link of the image data.您的http://example.com/api/photo的 URL 是图片数据的直接链接。
    • Your authorization script can be used for uploading a file to Google Drive.您的授权脚本可用于将文件上传到 Google Drive。
  • In this answer, as a sample script, the file is uploaded with uploadType=multipart .在此答案中,作为示例脚本,文件使用uploadType=multipart上传。 In this case, the maximum file size is 5 MB.在这种情况下,最大文件大小为 5 MB。 Please be careful this.请注意这一点。 When you want to upload the file with the large size, please check the resumable upload.当您要上传大尺寸文件时,请勾选可续传。 Ref 参考

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用谷歌驱动器 api 将文件上传到谷歌驱动器? - How I can upload file to google drive with google drive api? 如何将图像从 url 上传到 Google Drive? - How to upload an image from an url to Google Drive? 如何按名称将文件上传到特定的谷歌驱动器文件夹? - How can I upload a file to a particular google drive folder by name? 如何在流星上使用Google Drive API上传文件? - How do I upload files via using Google Drive API on meteor? Google Drive Api v3如何使用Google表格格式上传? 400响应 - Google Drive Api v3 how can I upload with google sheet formatting? 400 response 如何使用gapi和resumable上传文件将文件上传到Google云端硬盘? - How to upload files to Google drive using gapi and resumable uploads? 如何在保留其目录结构的情况下将文件夹上传到 Google Drive? - How can I upload a folder to Google Drive with keeping its directory structure? 如何在 Google Drive 的特定文件夹上使用 React Native 上传文档? - How can I upload Document using React Native on Google Drive's Specific Folder? 使用Google Drive API上传大文件 - Using Google Drive API to upload large files 如何将字符串作为文件上传到 Google Drive? - How do I upload a string as a file to Google Drive?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM