2016-06-22 66 views
0

我正嘗試使用PHP從Google雲端存儲中的存儲桶中下載文件。我可以很好地上傳。我嘗試了一些我在這裏找到的代碼,但是Laravel告訴我沒有找到類Google_Http_Request通過PHP從Google雲端存儲下載文件

這是我想要的代碼:

$request = new Google_Http_Request($object['mediaLink'], 'GET'); 
$signed_request = $client->getAuth()->sign($request); 
$http_request = $client->getIo()->makeRequest($signed_request); 
echo $http_request->getResponseBody(); 

回答

2

下面是如何使用最新版本的client library (v2.0)的下載對象的示例:

// Authenticate your API Client 
$client = new Google_Client(); 
$client->useApplicationDefaultCredentials(); 
$client->addScope(Google_Service_Storage::DEVSTORAGE_FULL_CONTROL); 

$storage = new Google_Service_Storage($client); 

try { 
    // Google Cloud Storage API request to retrieve the list of objects in your project. 
    $object = $storage->objects->get($bucketName, $objectName); 
} catch (Google_Service_Exception $e) { 
    // The bucket doesn't exist! 
    if ($e->getCode() == 404) { 
     exit(sprintf("Invalid bucket or object names (\"%s\", \"%s\")\n", $bucketName, $objectName)); 
    } 
    throw $e; 
} 

// build the download URL 
$uri = sprintf('https://storage.googleapis.com/%s/%s?alt=media&generation=%s', $bucketName, $objectName, $object->generation); 
$http = $client->authorize(); 
$response = $http->get($uri); 

if ($response->getStatusCode() != 200) { 
    exit('download failed!' . $response->getBody()); 
} 

// write the file (or do whatever you'd like with it) 
file_put_contents($downloadName, $response->getBody()); 

你可以找到其他樣本在此使用存儲API Samples Repository

還有一個更新的庫正在開發名爲gcloud-php,它有一些01致電Google雲端存儲時致電。

+0

我正在使用一些舊庫的代碼示例。你的代碼很棒!謝謝! – rsebjara

+0

您有關於如何上傳的示例嗎?這是我目前的問題。 – zundi

相關問題