2016-11-06 90 views
2

我試圖在谷歌雲平臺上設置存儲。老實說,現在我對做什麼感到困惑。使用php api在谷歌雲存儲中插入圖片

我正在使用以下代碼。

require_once("vendor/autoload.php"); 

use Google\Cloud\Storage\StorageClient; 

use Google\Cloud\Storage\StorageObject; 

use Google\Cloud\Storage; 

$projectId = 'YOUR_PROJECT_ID'; 

$storage = new StorageClient([ 
    'projectId' => 'xx', 
    'key'=> 'yy' 
]); 

$file_name = "imagename"; 

$obj = new StorageObject(); 

$obj->setName($file_name); 


    $storage->objects->insert(
     "kmapsimage", 
     $obj, 
     ['name' => $file_name, 'data' => file_get_contents("https://kmapst.blob.core.windows.net/images/kmap581b939a7a28c.jpeg"),'mimeType' => 'image/jpeg'] 
); 

在執行函數時出現以下錯誤。

Argument 1 passed to Google\Cloud\Storage\StorageObject::__construct() must implement interface Google\Cloud\Storage\Connection\ConnectionInterface, none given, called in /var/www/html/test_imageupload.php on line 35 and defined in /var/www/html/vendor/google/cloud/src/Storage/StorageObject.php on line 71 

我只是想上傳圖片到谷歌存儲。

回答

2

該文檔顯示simpler方式

鑑於你的相關代碼:

$storage = new StorageClient([ 
    'projectId' => 'xx', 
    'key'=> 'yy' 
]); 

//使用上傳方法似乎有點更簡單

$bucket = $storage->bucket('bucket-name'); 
$file = fopen('path/to/file', 'r'); 
$object = $bucket->upload($file, ['name' => 'fileName']); 
2

確保您可以使用您可以從js中的GCP下載的憑據n格式。

在頭我有這樣的:

putenv("GOOGLE_APPLICATION_CREDENTIALS=".$_SERVER['DOCUMENT_ROOT']."/uploads/my-fe1tyui28f4f.json"); 

require $_SERVER['DOCUMENT_ROOT'].'/uploads/php-docs-samples/storage/api/vendor/autoload.php'; 

use Google\Cloud\Storage\StorageClient; 

$storage = new StorageClient(); 

// Primera imagen...... 
$file = fopen($_SERVER['DOCUMENT_ROOT']."/uploads/".$filename."_200.jpg", 'r'); 
$bucket = $storage->bucket("my_bucket"); 
$object = $bucket->upload($file, ['name' => $dir."/".$filename."_200.jpg"]); 
$object = $bucket->object($dir."/".$filename."_200.jpg"); 
$object->update(['acl' => []], ['predefinedAcl' => 'PUBLICREAD']); 

我希望這有助於!