2016-06-22 8 views
0

Ich versuche, Dateien aus meinem Bucket in Google Cloud Storage mit PHP herunterzuladen. Ich kann gut hochladen. Ich habe einige Codes ausprobiert, die ich hier gefunden habe, aber Laravel sagt mir, dass die Klasse Google_Http_Request nicht gefunden wird.Datei von Google Cloud Storage über PHP herunterladen

Dies ist der Code Ich versuche:

$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(); 

Antwort

2

Hier ist ein Beispiel dafür, wie ein Objekt laden Sie die neueste Version der client library (v2.0) mit:

// 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()); 

Sie andere Proben finden bei der Verwendung der Speicher-API in dieser Samples Repository

Es gibt auch eine neuere Bibliothek namens gcloud-php entwickelt, die einige 01 hatbeim Aufruf von Google Cloud Storage.

+0

Ich verwendete einige Codebeispiele aus einer alten Bibliothek. Dein Code funktioniert großartig! Vielen Dank! – rsebjara

+0

Haben Sie ein Beispiel dafür, wie Sie hochladen? Das ist mein Problem im Moment. – zundi