我正在使用api将文件上传到谷歌驱动器,但实际上我有以下方法:
我有这个编码工作我上面提到的。 (代码在一个框架内)
public function actionCallgdrive() {
$client = new Google_Client();
$client->setApplicationName("Google Drive");
$client->setAuthConfig((dirname(__FILE__)) . '/client_secret.json');
$client->setRedirectUri('http://' . $_SERVER['SERVER_NAME'] . Url::to(['content/callgdrive']));
$client->addScope(Google_Service_Drive::DRIVE);
$client->setAccessType('offline');
if (!isset($_GET['code'])) {
$auth_url = $client->createAuthUrl();
$this->redirect($auth_url);
} else {
$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();
$this->redirect(['content/google-drive']);
}
}
public function actionGoogleDrive($filename) {
$client = new Google_Client();
$client->setApplicationName("Google Drive");
$client->setAuthConfig((dirname(__FILE__)) . '/client_secret.json');
$client->addScope(Google_Service_Drive::DRIVE);
$client->setAccessType('offline');
if (isset($_SESSION['access_token']) && $_SESSION['access_token']) {
$client->setAccessToken($_SESSION['access_token']);
if ($client->isAccessTokenExpired()) {
$refreshToken = $this->getRefreshTokenFromDatabase();
$client->refreshToken($refreshToken);
$newAccessToken = $client->getAccessToken();
$newAccessToken['refresh_token'] = $refreshToken;
$this->setRefreshTokenIntoDatabase($newAccessToken);
$_SESSION['access_token'] = $newAccessToken;
}
set_time_limit(0);
$gdrive_service = new Google_Service_Drive($client);
$file = new Google_Service_Drive_DriveFile();
$folderId = 'dfghjtyguygyuuuuuuuuuuu';
$file->setName($filename);
$file->setParents([$folderId]);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$request = $gdrive_service->files->create($file);
$media = new Google_Http_MediaFileUpload(
$client, $request, 'application/zip', null, true, $chunkSizeBytes
);
$media->setFileSize(filesize(Yii::getAlias('@webroot').'/content/' . $filename));
$status = false;
$handle = fopen(Yii::getAlias('@webroot').'/content/' . $filename, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if ($status != false) {
$result = $status;
$fileId = $result->getId();
}
fclose($handle);
$client->setDefer(false);
} else {
$this->actionCallgdrive();
}
}
public function actionUpload()
{
$model = new UploadForm();
if (Yii::$app->request->post()) {
$model->file = UploadedFile::getInstance($model, 'file');
if ($model->validate()) {
$model->file->saveAs(Yii::getAlias('@webroot').'/content/' . $model->file->baseName . '.' . $model->file->extension);
$filename = $model->file->baseName . '.' . $model->file->extension;
$this->actionGoogleDrive($filename);
}
}
return $this->render('upload', ['model' => $model]);
}
所以,我的问题是:我可以使用文件输入直接将文件发送到谷歌驱动器吗?如果是的话,我该怎么做?
答案 0 :(得分:0)
“我可以使用文件输入直接将文件发送到谷歌驱动器吗?如果是,我该怎么做?”
我认为使用Files.insert方法并将文件的MIME类型指定为application / vnd.google-apps.drive-sdk是可行的。
由于您正在寻找PHP示例,请从指南中尝试以下内容:
function insertFile($service, $title, $description, $parentId, $mimeType, $filename) {
$file = new Google_Service_Drive_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);
// Set the parent folder.
if ($parentId != null) {
$parent = new Google_Service_Drive_ParentReference();
$parent->setId($parentId);
$file->setParents(array($parent));
}
try {
$data = file_get_contents($filename);
$createdFile = $service->files->insert($file, array(
'data' => $data,
'mimeType' => $mimeType,
));
// Uncomment the following line to print the File ID
// print 'File ID: %s' % $createdFile->getId();
return $createdFile;
} catch (Exception $e) {
print "An error occurred: " . $e->getMessage();
}
}