Search code examples
laravelgoogle-cloud-storageimage-upload

Uploading image file to google cloud using Laravel


Hi i have trying to upload image file to google cloud storage using laravel API. i have integrated google sdk via composer and i try to hit with postman i am getting the url and get stored in my database but the image file is not uploaded in the folder in google cloud .i created a folder with name 'avatars' in by bucket. here is my code. this is my controller

public function updateAvatar (AvatarUploadRequest $request) {
    $me = Auth::user();
    $disk = Storage::disk('gcs');
    $url = $disk->url('avatars'. "/" . $me->uuid . ".jpg");
    $me->avatar = $url;
    $me->save();
    return $this->prepareItem($me);
}

this is my filesystems.php file

'gcs' => [
     'driver' => 'gcs',
     'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'my-project-id'),
     'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null),
     'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'my-bucket-name'),
     'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null),
     'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI',
     'https://console.cloud.google.com/storage/browser/my-project-id/'),
],

This all i have done. did i missing anything? any additional configuration needed?


Solution

    1. First Create an Account in google cloud storage For that you will need credit card details but it won't charge, when we were not gooing for "Upgrade" because this is to make sure that it is not Robot.

    2. Create a Project in google cloud storage, here for example Project Name is "My-project".

    3. Create a Bucket in the Project "My-project". for example here i created "My-buckets"

    4. Create a Folder in the bucket for example here i created a folder name "avatars".

    5. Go to the optinon IAM & Admin => Service accounts => Create Service account => Put Service Account name should ne the Bucket name "my-buckets".=> Check Furnish a new private key and save then a new json file will download and put that file in the project.Here i rename it as my-buckets.json.

    6. In app/Http/Controllers/Api/v2/UserController.php:

      public function updateAvatar (AvatarUploadRequest $request) {
          $me = Auth::user();
          $file = $request->file('avatar');
          $name= $me->uuid . ".".$file->get ClientOriginalExtension();
          $filePath = 'avatars/' . $name;
          $disk = Storage::disk('gcs')->put($filePath, file_get_contents($file));
          $gcs = Storage::disk('gcs');
          $url = $gcs->url('avatars'. "/" . $me->uuid . ".jpg");
          $me->avatar = $url;
          $me->save();
          return $this->prepareItem($me);
      }
      
    7. In config/filesystems.php set a driver for the google cloud

      'gcs' => [
            'driver' => 'gcs',
            'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'my-project-209405'),
            // optional: /path/to/service-account.json
            'key_file' => env('GOOGLE_APPLICATION_CREDENTIALS', './my-buckets.json'),
            'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'my-buckets'),
            // optional: /default/path/to/apply/in/bucket
            'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null),
            // see: Public URLs below
            'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', 'https://storage.googleapis.com/my-buckets/'),
       ],
      
    8. Add the Path Of my-buckets.json we got at Step 5 to the Key_file

    9. Dowload Google SDK Console https://cloud.google.com/sdk/

    10. First we dont have the Access to the Account which the google cloud is created ,To get access we need to run google cloud command in google SDK console Run : gcloud auth login Then it will open the brouser a asking the gmail account which we created the google cloud storage and allow the permission for google sdk to access, Then it will show the current project that we are, in the console.

    11. Run the command to enable the public accessibility of the object. The URL that we are getting and stored in the database is not having publicly access. Run : gsutil iam ch allUsers:objectViewer gs://my-buckets