Monday 9 July 2018

How to upload files to Google Cloud with PHP?

How to upload files to Google Cloud with PHP?

Question: What are different ways to access Google Services?
  1. Using Client Libraries like PHP, Node, Java etc
  2. Using Gcloud tool command line tool, in this you need to download the cloud SDK which is available for windows, mac and linux.
  3. Using existing command line, similar to above 2nd point.



Question: Question: How to start with client libraries?
  1. Set up a "GCP Console project" (Skip, if already done).
    https://cloud.google.com/resource-manager/docs/creating-managing-projects
    Create or select a project.
    Enable the Google Speech-to-Text API for that project.
    Create a service account.
    Download a private key as JSON (Important, we need this).
  2. Set the environment variable GOOGLE_APPLICATION_CREDENTIALS to the private key JSON file that contains your service account key(which you have downloaded)
  3. Install the client library



Question: What is price cost for storage?
https://cloud.google.com/storage/pricing-summary/


Question: What are the use cases for storage?
  1. Media storage for website
  2. Streaming videos and music
  3. Mobile app development
  4. Video transcoding
  5. General data analytics and compute
  6. Storage data for Machine learning
  7. Backup data



Question: What are the use cases for storage?
  1. Multi-Regional: For highest availability of frequently accessed data
  2. Regional: For data accessed frequently within a region (cheaper)
  3. Nearline: data which access once a month (more cheaper)
  4. Coldline: data which accessed once a year (cheapest)



Question: What is price cost for storage?
https://cloud.google.com/storage/pricing-summary/


Question: Who are the Partners of Google storage?
https://cloud.google.com/storage/partners/


Question: How to setup of Google storage for PHP?
Step 1 Go to folder where you want to install

Step 2 Install composer (Skip if already)
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
php composer-setup.php
php -r "unlink('composer-setup.php');"


Step 3: Install the library
php composer.phar require google/cloud-storage



Question: How to create folder in Google cloud?
require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;

#Setup the Private Key JSON File in Environment
$credentialFilePath='private_key_JSON_file.json;
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentialFilePath);

# Your Google Cloud Platform project ID
$projectId = 'projectName';

# Instantiates a client
$storage = new StorageClient([
    'projectId' => $projectId
]);

# The name for the new bucket
$bucketName = 'mynew-test-bucket';

# Creates the new bucket
$bucket = $storage->createBucket($bucketName);

Question: How to upload file in Google cloud folder?

require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;

#Setup the Private Key JSON File in Environment
$credentialFilePath='private_key_JSON_file.json;
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentialFilePath);

# Your Google Cloud Platform project ID
$projectId = 'projectName';

$storage = new StorageClient();
$bucket = $storage->bucket($projectId);

# Upload a file to the bucket.
$bucket->upload(
    fopen('/downloaded/file.mp3','r')
); 



Question: How to delete file from Google cloud folder?

require_once 'vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;

#Setup the Private Key JSON File in Environment
$credentialFilePath='private_key_JSON_file.json;
putenv('GOOGLE_APPLICATION_CREDENTIALS='.$credentialFilePath);

# Your Google Cloud Platform project ID
$projectId = 'projectName';

$storage = new StorageClient();
$bucket = $storage->bucket($projectId);

$object = $bucket->object($sourceFile); 
$object->delete(); 



Question: What is office link for gcloud storage?
https://cloud.google.com/storage/


Question: What is Github link for gcloud storage (with PHP)?
https://github.com/GoogleCloudPlatform/google-cloud-php