Created
April 5, 2014 20:25
-
-
Save AidanThreadgold/9997654 to your computer and use it in GitHub Desktop.
Upload a file to google cloud
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once("src/Google/Client.php"); | |
require_once("src/Google/Service/Storage.php"); | |
/** | |
* Connect to Google Cloud Storage API | |
*/ | |
$client = new Google_Client(); | |
$client->setApplicationName("App Name"); | |
// $stored_access_token - your cached oauth access token | |
if( $stored_access_token ) { | |
$client->setAccessToken( $stored_access_token ); | |
} | |
$credential = new Google_Auth_AssertionCredentials( | |
"service account email address", | |
['https://www.googleapis.com/auth/devstorage.read_write'], | |
file_get_contents("path/to/private-key-certificate.p12") | |
); | |
$client->setAssertionCredentials($credential); | |
if($client->getAuth()->isAccessTokenExpired()) { | |
$client->getAuth()->refreshTokenWithAssertion($credential); | |
// Cache the access token however you choose, getting the access token with $client->getAccessToken() | |
} | |
/** | |
* Upload a file to google cloud storage | |
*/ | |
$storage = new Google_Service_Storage($client); | |
$file_name = "what you want your file to be named on Google Cloud Storage"; | |
$obj = new Google_Service_Storage_StorageObject(); | |
$obj->setName($file_name); | |
$storage->objects->insert( | |
"bucket name", | |
$obj, | |
['name' => $file_name, 'data' => file_get_contents("local path of the file to upload"), 'uploadType' => 'media'] | |
); |
how do i stored the link in db.. to load my contents
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For this library : https://github.com/GoogleCloudPlatform/google-cloud-php#google-cloud-storage-ga
Use as following :
$bucket->upload(
fopen('data3.txt', 'r'), ['predefinedAcl' => 'publicRead']
);