使用cURL上传到Google Drive,并设置名称和位置 [英] Upload to google drive using cURL and set name and location

查看:1177
本文介绍了使用cURL上传到Google Drive,并设置名称和位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cURL将文件上传到Google云端硬盘。



我的基本脚本如下所示:

  curl 
--silent
--request POST
--data-binary@c:\temp\myfile .jpg
-H授权:Bearer abcdefg
-HContent-Type:image / jpeg
https://www.googleapis.com/upload/drive/v2/文件?uploadType = media

使用这些链接作为参考:



将文件上传到云端硬盘并设置文件名< a>



https://gist.github。 com / deanet / 3427090



https://developers.google.com/drive/web/manage-uploads



我注意到这可能是必需的, (请参阅链接)

  -HSlug:myfile3.jpg



但是这不会影响我是否把它。



使用cURL帮助,我注意到这可能会有所帮助:

  -OL MyFolder\myfile2.jpeg 


$ b

我也注意到,可能需要string {title:file title},但我不知道如何将其嵌入到使用cURL。基于上面的链接,我想我可以只是将它附加在一个标题的末尾,就在身体之前。所以,我试图得到聪明,并改变内容类型规范为:

  -HContent-Type:image / jpeg {title:myfile4}

...但它也没有效果。



很奇怪,Google自己的文档不正确,因此,我怀疑他们曾经使用过的方法现在已过时。链接中的授权规范错误。您必须使用Bearer令牌,而不是GoogleLogin。我提到了此链接:



https: //developers.google.com/gdata/articles/using_cURL



我的整个脚本看起来像这样。

  curl 
- 上传,但不是我想要的方式, silent
--request POST
--data-binary@c:\temp\myfile.jpg
-OL MyFolder\myfile2.jpeg
-H Slug:myfile3.jpg
-H授权:Bearer abcdefg
-HContent-Type:image / jpeg {title:myfile4}
https: /www.googleapis.com/upload/drive/v2/files?uploadType=media

我实现我在一个地方指定了myfile2.jpg,在另一个地方指定了myfile3.jpg,在另一个地方指定了myfile4。



任何帮助都将非常感激。

解决方案

您的问题是,您正在做的媒体内容没有元数据。您有两种方法前进: -


  1. 启动元数据以创建空文件并捕获其文件ID。然后使用file-id进行内容上传

  2. 在一个POST中创建一个包含元数据和内容的multipart mime正文

就个人而言,我会选择选项1,因为它分离出了两个问题,但是两者同样有效。这些选项在 https://developers.google.com/drive/web/manage-上传


I'm attempting to upload a file to Google Drive using cURL. The files get uploaded, but, only in the root and are always named "Untitled".

My base script looks like this:

curl 
    --silent 
    --request POST 
    --data-binary "@c:\temp\myfile.jpg"         
    -H "Authorization: Bearer abcdefg" 
    -H "Content-Type: image/jpeg
    "https://www.googleapis.com/upload/drive/v2/files?uploadType=media" 

Using these links as references:

Upload file to Drive and set file name

https://gist.github.com/deanet/3427090

https://developers.google.com/drive/web/manage-uploads

I note that this might be required, which is defined on Google's documentation (see link)

-H "Slug: myfile3.jpg" 

But this has no effect whether or not I put it in.

Using cURL help, I note this might be helpful:

 -OL MyFolder\myfile2.jpeg

...but this has no effect, either.

I also note that the string {title:file title} may be required, but I don't know how to embed it in using cURL. Based on the above links, I thought I could just append it at the end of one of the headers, just before the body. So, I tried to get clever and changed the content-type specification to this:

-H "Content-Type: image/jpeg {"title":"myfile4"}" 

...but it also has no effect.

Curiously, Google's own documentation is incorrect, so, I suspect that the method they once used is now out of date. The authorization specification in the link is wrong. You must use Bearer tokens, not "GoogleLogin". I referred to this link:

https://developers.google.com/gdata/articles/using_cURL

My entire script looked like this. It uploads, but, not where I want it and not named the way I want it.

curl 
    --silent 
    --request POST 
    --data-binary "@c:\temp\myfile.jpg"         
    -OL MyFolder\myfile2.jpeg
    -H "Slug: myfile3.jpg" 
    -H "Authorization: Bearer abcdefg" 
    -H "Content-Type: image/jpeg {"title":"myfile4"}" 
    "https://www.googleapis.com/upload/drive/v2/files?uploadType=media" 

I realize I specified "myfile2.jpg" in one place, "myfile3.jpg" in another, and "myfile4" in yet another. It makes no difference whether they are matched or specified at all.

Any help would be most appreciated!

解决方案

Your problem is that you are doing the media content without the meta data. You have two ways forward :-

  1. POST the metadata to create the empty file and capture its file-id. Then use the file-id for a content upload
  2. Create a multipart mime body which contains both the metadata and content in a single POST

Personally, I'd go with option 1 as it separates out the two concerns, but both are equally valid. The options are described at https://developers.google.com/drive/web/manage-uploads

这篇关于使用cURL上传到Google Drive,并设置名称和位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆