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 by using Web Apps, you can achieve what you want to do. The flow of this workaround is as follows.
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);
}
https://script.google.com/macros/s/#####/exec
.
url=http://example.com/file.doc
.file.doc
by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc
using your browser.
If this was not useful for you, I'm sorry.
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.
<?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);
?>
$.get("https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc", function(res) {
console.log(res);
});
When you want to make users upload files to own Google Drive, please deploy Web Apps by the following flow.
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);
}
https://script.google.com/macros/s/#####/exec
.
url=http://example.com/file.doc
.file.doc
by accessing this URL https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc
using your browser.
https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc
, the login screen to Google is displayed.file.doc
is uploaded to user's Google Drive. Because the script is run as each user.https://script.google.com/macros/s/#####/exec?url=http://example.com/file.doc
as a link.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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With