2
votes

Im trying to integrate Google Cloud Storage for file uploads and serving in a laravel app that is stored in Google Compute Engine.

What would be the best practice for that? Is it the right way of using the Google Cloud Storage?

Should I use the Storage facade for that? or a Job for copying local file uploads to the cloud?

Thanks

2

2 Answers

1
votes

You can use the Google APIs Client Library for PHP to send requests to the Google Cloud Storage JSON API. Visit this link for example.

1
votes

Better approach is to directly uploading to cloud storage.

As directly uploading needs to upload file data only once in other process you will have to first upload it to some temp file and then from that temp file to cloud storage. So as data transfer is more it would be slower and image can be unavailable also till the time job runs and move it from temp location to cloud storage.

Now let's address how to upload directly in cloud storage using Laravel. Below steps can be followed to achieve it :-

  1. Import CloudStorageTools
    use google\appengine\api\cloud_storage\CloudStorageTools;

  2. Create bucket url to directly uploading file
    $bucket_options = ['gs_bucket_name' => $my_bucket]; $cloud_storage_upload_url = CloudStorageTools::createUploadUrl('/upload/handler', $bucket_options);

  3. Now above generated $cloud_storage_upload_url can be used in action tag of form to upload file directly.
    Example:-
    <form action="{{ upload_url }}" enctype="multipart/form-data" method="post">

Footnote :- Upload url we generated would be only accessible till 10 minutes of it's creation.

Also documentation for same can be found here.