谷歌云存储PHP

时间:2017-06-08 06:04:42

标签: php google-cloud-storage

嗨我运行以下脚本时遇到一个奇怪的错误。它返回此错误消息

Parse error: syntax error, unexpected 'new' (T_NEW) in /home/apipetfindr/public_html/sys/v1/google_cloud_storeage_upload.php on line 9

我想知道为什么我的代码的这部分不起作用:

private $storage = new StorageClient

我知道我需要调用我已尝试公开的新功能而不是私有功能但没有成功。如果有人能指出我的方向是正确的那么好。

<?php
# Includes the autoloader for libraries installed with composer
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;


class googleupload{

  private $storage = new StorageClient([
      'projectId' => 'xxxxxxxxxx'
  ]);

  private $bucket = $storage->bucket('BUCKET_NAME');

  // Upload a file to the bucket.
  //$bucket->upload(
  //    fopen('/data/file.txt', 'r')
  //);


  public function uploads($_FILE, $uid){
        $obj = new Google_Service_Storage_StorageObject();
        $obj->setName($file_name);

        $resp = $storage->objects->insert(
            "bucketname",
            $obj,
            array('name' => $file_name, 'data' => file_get_contents("300mb-file.zip"), 'uploadType' => 'media')
        );

  }
}
/*
// Download and store an object from the bucket locally.
$object = $bucket->object('file_backup.txt');
$object->downloadToFile('/data/file_backup.txt');
*/
?>

1 个答案:

答案 0 :(得分:1)

不允许使用new Object(x, y, [z])之类的内容作为类属性的默认值。如果您希望始终具有相同的属性值,例如使用projectId xxxx你的对象,你可以在你的构造函数中添加这样的东西:

public function __construct(){
  $this->storage = new StorageClient(['projectId' => 'xxxxxxxx']);
  $this->bucket = $this->storage->bucket('BUCKET_NAME');
}