要上传到Google云端硬盘的网址 [英] URL to upload to Google Drive

查看:116
本文介绍了要上传到Google云端硬盘的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何网址可以直接上传到Google云端硬盘?例如,我在我的服务器 http://example.com/file.doc 上有一个文件,我想用类似的方式将它上传到Google Drive: http://google.com/?upload=http://example.com/file.doc



什么是方法要做到这一点?

解决方案

这个解决方法如何?不幸的是,没有网址可以直接上传未经Google API授权的文件。所以我提出一个解决方法。我认为这种方法可能很容易实现你想要做的事情。请把它想成几个答案之一。



我认为使用网络应用程序,你可以实现你想做的事情。此解决方法的流程如下。

流程:



1。示例脚本




  • 创建独立脚本。


    • 您也可以在绑定脚本中执行此流程。此处使用独立脚本。


  • 复制并粘贴以下示例脚本并将其保存。



请运行 doGet(e)。由此,虽然发生了错误,但可以完成授权过程。

 函数doGet(e){
var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();
var id = DriveApp.createFile(blob).getId();
返回ContentService.createTextOutput(id);
}



2。部署Web应用程序。




  • 在脚本ediror上的菜单栏中,选择发布 - >部署为Web应用程序。
  • 在项目版本:输入字符串。
  • 在执行应用程序为:选择我。

  • 在谁有权访问该应用程序:中,选择任何人,甚至是匿名。

  • 点击部署按钮。当前的网络应用程序URL:。该网址为 https://script.google.com/macros/s/#####/exec


    • 您可以使用此URL上传文件。 确定。



    3。从URL上传文件
    $ b

    • 查询参数是 url = http://example.com/file.doc

    • 您可以通过访问此URL https:/上传 file.doc /script.google.com/macros/s/#####/exec?url=http://example.com/file.doc 使用您的浏览器。


      • 在此示例脚本中,上传文件是在Google云端硬盘的根文件夹中创建的。
      • li>


      注意:




      • 关于Web Apps,if您更新脚本,请重新部署Web Apps作为新版本。这样,反映了更新的脚本。

      • 这个示例脚本非常简单。所以请将其修改为您的环境。当时,如果您有任何问题,请随时告诉我。

      • 在这种情况下,当用户使用Web Apps URL上传文件时,文件会上传到Google驱动部署Web应用程序的所有者。因为该脚本仅作为所有者运行。



      如果这对于您好,我很抱歉。

      编辑:



      据我所知,您想要使用URL上传文件PHP和JavaScript(我在这里使用jQuery)。如果我的理解错误,请告诉我。需要 doGet(e)的Google Apps脚本才能使用这些脚本。首先,请复制并粘贴 doGet(e)并部署Web Apps。对于这两种脚本,Google Drive上创建的文件的文件ID均作为响应返回。



      通过php上载



       <?php 
      $ url ='https://script.google.com/macros/s/#####/exec?url=http:/ /example.com/file.doc';
      $ curl = curl_init();
      curl_setopt($ curl,CURLOPT_URL,$ url);
      curl_setopt($ curl,CURLOPT_CUSTOMREQUEST,'GET');
      curl_setopt($ curl,CURLOPT_RETURNTRANSFER,true);
      curl_setopt($ curl,CURLOPT_FOLLOWLOCATION,true);
      $ response = curl_exec($ curl);
      echo $ response;
      curl_close($ curl);
      ?>



      通过jQuery上载



        $。get(https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc,函数( res){
      console.log(res);
      });



      参考文献:





      编辑2:



      如果您想让用户将文件上传到自己的Google云端硬盘,请通过以下流程部署Web Apps 。

      流程:



      1。示例脚本




      • 创建独立脚本。


        • 您也可以在绑定脚本中执行此流程。此处使用独立脚本。


      • 复制并粘贴以下示例脚本并将其保存。



      请运行 doGet(e)。由此,虽然发生了错误,但可以完成授权过程。

       函数doGet(e){
      var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();
      var id = DriveApp.createFile(blob).getId();
      返回ContentService.createTextOutput(id);
      }



      2。部署Web应用程序。




      • 在脚本ediror上的菜单栏中,选择发布 - >部署为Web应用程序。
      • 在项目版本:输入字符串。 在执行应用程序为:中,选择访问网络应用程序的用户

      • 在谁有权访问应用程序:中选择任何人
      • 点击部署按钮。
      • 请复制当前的网络应用程序URL:。该网址为 https://script.google.com/macros/s/#####/exec


        • 您可以使用此URL上传文件。 确定。



        3。从URL上传文件
        $ b

        • 查询参数是 url = http://example.com/file.doc

        • 您可以通过访问此URL https:/上传 file.doc /script.google.com/macros/s/#####/exec?url=http://example.com/file.doc 使用您的浏览器。


          • 在此示例脚本中,上传文件是在Google云端硬盘的根文件夹中创建的。
          • li>
          • 当用户访问的网址时https://script.google.com/macros/s/#####/exec?url=http://example .com / file.doc ,则会显示Google的登录屏幕。

          • 登录Google后,会显示授权屏幕。

          • 通过授权, file.doc 会上传到用户的Google云端硬盘。因为脚本是以每个用户的身份运行的。



          注意:




          • 只有一个用户。请在所有用户中共享Web应用程序的URL。

          • 在这种情况下,用户使用每个用户的浏览器,因为浏览器需要授权过程。


            • 如果您将其用作HTML,您可以将的网址设置为https://script.google.com/macros/ s / ##### / exec?url = http://example.com/file.doc 作为链接。


          • 关于Web Apps,如果您更新脚本,请重新部署Web Apps作为新版本。这样,反映了更新的脚本。



          编辑3:



          在使用Web Apps的情况下,当< div class =google-uploaddata-url =http://example.com/file.doc> 上传时,示例脚本如下。

          > file.doc 上传到用户的云端硬盘。为了使用此示例脚本,请使用通过使用上面的编辑2部署Web应用程序检索到的URL。



          在这种情况下,登录屏幕会显示给每个用户。用户需要在登录后通过点击屏幕上的按钮授权使用API​​。这是你想要的吗?

           < html> 
          < head>
          < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js>< / script>
          < / head>
          < body>
          < div class =google-uploaddata-url =http://example.com/file.doc>
          < span style =background-color:#ddd>上传< / span>
          < / div>
          < script>
          $(function(){
          $(。google-upload)。click(function(){
          var url =https://script.google.com/macros/ s / ##### / exec; //请在这里输入网址
          var withQuery = url +?url =;
          window.open(withQuery + $('。google-上传')。attr(data-url),_blank,width = 600,height = 600,scrollbars = 1);
          });
          });
          < / script>
          < / body>
          < / html>


          Is there any URL I could use to directly upload to Google Drive? For example, I have a file on my server http://example.com/file.doc and I want to upload it to Google Drive with something like: http://google.com/?upload=http://example.com/file.doc

          What's the approach to do this?

          解决方案

          How about this workaround? Unfortunately, there are no URLs for directly uploading files without authorization on Google API. So I propose a workaround. I thought that this method may be easy for achieving what you want to do. Please think of this as one of several answers.

          I think that using Web Apps, you can achieve your what you want to do. The flow of this workaround is as follows.

          Flow :

          1. Sample script

          • Create standalone script.
            • Also you can do this flow at bound scripts. Here, a standalone script is used.
          • Copy and paste following sample script, and save it.

          Please run doGet(e). By this, although an error occurs, the authorization process can be done.

          function doGet(e) {
            var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();
            var id = DriveApp.createFile(blob).getId();
            return ContentService.createTextOutput(id);
          }
          

          2. Deploy Web Apps.

          • On script ediror, at menu bar, select Publish -> Deploy as Web App.
          • At "Project version:", input string.
          • At "Execute the app as:", select "Me".
          • At "Who has access to the app:", select "Anyone, even anonymous".
          • Click "Deploy" button.
          • Please copy "Current web app URL:". The URL is https://script.google.com/macros/s/#####/exec.
            • You can upload files using this URL.
          • Click "OK".

          3. Upload a file from URL

          • Query parameter is url=http://example.com/file.doc.
          • You can upload file.doc by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc using your browser.
            • In this sample script, the upload file is created on root folder on your Google Drive.

          Note :

          • About Web Apps, if you update your script, please redeploy Web Apps as new version. By this, the updated script is reflected.
          • This sample script is very simple. So please modify this to your environment. At that time, if you have any problems, feel free to tell me.
          • In this case, when users upload files using the Web Apps URL, the files are uploaded to Google Drive of owner who deployed Web Apps. Because the script is run as only owner.

          If this was not useful for you, I'm sorry.

          Edit :

          I understand that you want to upload files from URL using php and javascript (I used jQuery here.). If my understanding is wrong, please tell me. doGet(e) of Google Apps Script is required to use these script. At first, please copy and paste doGet(e) and deploy Web Apps. For both script, the file ID of created file on Google Drive is returned as the response.

          Upload by php

          <?php
              $url = 'https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc';
              $curl = curl_init();
              curl_setopt($curl, CURLOPT_URL, $url);
              curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
              curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
              curl_setopt($curl,CURLOPT_FOLLOWLOCATION, true);
              $response = curl_exec($curl);
              echo $response;
              curl_close($curl);
          ?>
          

          Upload by jQuery

          $.get("https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc", function(res) {
              console.log(res);
          });
          

          References :

          Edit 2 :

          When you want to make users upload files to own Google Drive, please deploy Web Apps by the following flow.

          Flow :

          1. Sample script

          • Create standalone script.
            • Also you can do this flow at bound scripts. Here, a standalone script is used.
          • Copy and paste following sample script, and save it.

          Please run doGet(e). By this, although an error occurs, the authorization process can be done.

          function doGet(e) {
            var blob = UrlFetchApp.fetch(e.parameters.url).getBlob();
            var id = DriveApp.createFile(blob).getId();
            return ContentService.createTextOutput(id);
          }
          

          2. Deploy Web Apps.

          • On script ediror, at menu bar, select Publish -> Deploy as Web App.
          • At "Project version:", input string.
          • At "Execute the app as:", select "User accessing the web app".
          • At "Who has access to the app:", select "Anyone".
          • Click "Deploy" button.
          • Please copy "Current web app URL:". The URL is https://script.google.com/macros/s/#####/exec.
            • You can upload files using this URL.
          • Click "OK".

          3. Upload a file from URL

          • Query parameter is url=http://example.com/file.doc.
          • You can upload file.doc by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc using your browser.
            • In this sample script, the upload file is created on root folder on your Google Drive.
          • When users access the URL of https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc, the login screen to Google is displayed.
          • After logged in Google, the authorization screen is displayed.
          • By authorized it, file.doc is uploaded to user's Google Drive. Because the script is run as each user.

          Note :

          • Please deploy Web Apps by only one user. And please share the URL of Web Apps among all users.
          • In this case, users use the browser of each user, because the authorization process is required the browser.
            • If you use this as a HTML, you can put the URL of https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc as a link.
          • About Web Apps, if you update your script, please redeploy Web Apps as new version. By this, the updated script is reflected.

          Edit 3:

          In the case of use of Web Apps, when <div class="google-upload" data-url="http://example.com/file.doc"> is used, the sample script is as follows.

          When users click Upload, file.doc is uploaded to user's Drive. In order to use this sample script, please use the URL retrieved by deploying Web Apps using above "Edit 2".

          In this case, the login screen is displayed to each user. Users are required to authorize to use APIs after login by click the button on the screen. Is this what you want?

          <html>
            <head>
              <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.js"></script>
            </head>
            <body>
              <div class="google-upload" data-url="http://example.com/file.doc">
                <span style="background-color: #ddd">Upload</span>
              </div>
              <script>
                $(function() {
                  $(".google-upload").click(function() {
                    var url = "https://script.google.com/macros/s/#####/exec"; // Please input the URL here.
                    var withQuery = url + "?url=";
                    window.open(withQuery + $('.google-upload').attr("data-url"), "_blank", "width=600,height=600,scrollbars=1");
                  });
                });
              </script>
            </body>
          </html>
          

          这篇关于要上传到Google云端硬盘的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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