1
votes

I am using laravel-google-cloud-storage package from github in my Laravel project to upload files to google cloud storage. I've a JSON file with credentials, project id and bucket list in my .env file.

.env File

GOOGLE_CLOUD_PROJECT_ID='myproject-123456'
GOOGLE_CLOUD_KEY_FILE=/var/www/project/credintials.json
GOOGLE_CLOUD_STORAGE_BUCKET=minisite_event

.json credentials

{
  "type": "service_account",
  "project_id": "myproject-123456",
  "private_key_id": "MY_PROJECT_KEY_ID",
  "private_key": "-----BEGIN PRIVATE KEY-----\nTHEWHOLEKEYHEREWITHVERYVERYLONGTEXTLIKEANYOTHERPRIVATEKEYFILE=\n-----END PRIVATE KEY-----\n",
  "client_email": "TEST@PROJECT_ID.iam.gserviceaccount.com",
  "client_id": "99112211221122112211",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/ATESTSTRING.iam.gserviceaccount.com"
}

Filesystem Config

'gcs' => [
            'driver' => 'gcs',
            'project_id' => env('GOOGLE_CLOUD_PROJECT_ID', 'your-project-id'),
            'key_file' => env('GOOGLE_CLOUD_KEY_FILE', null), // optional: /path/to/service-account.json
            'bucket' => env('GOOGLE_CLOUD_STORAGE_BUCKET', 'your-bucket'),
            'path_prefix' => env('GOOGLE_CLOUD_STORAGE_PATH_PREFIX', null), // optional: /default/path/to/apply/in/bucket
            'storage_api_uri' => env('GOOGLE_CLOUD_STORAGE_API_URI', null), // see: Public URLs below
        ]

In my Laravel, I am trying to put the file in filesystem

$disk = Storage::disk('gcs');
$path = $disk->putFile('event_'.$id, $request->file('qqfile'));

Then I get this error:

NotFoundException
{
    "error": {
         "errors": [
            {
                "domain": "global",
                "reason": "notFound",
                "message": "Not Found"
            }
         ],
    "code": 404,
    "message": "Not Found"
    }
}

I am not sure what I am doing wrong. Also the package is not well documented and this is the best package I could find that already implemented Filesystem.

1
did you create the bucket?jdp
Did you set permission at the bucket level [cloud.google.com/storage/docs/cloud-console#_bucketpermission], so that users can create or view objects in your bucket? If so can you share which permissions are you setting for your bucket?Gonca
@GonçaloAlbino actually my bucket name was wrong :( I misunderstood a folder name inside the bucket as bucket name.Ariful Haque
@ArifulHaque May I ask you to post the solution as an answer?Gonca

1 Answers

2
votes

Here is the answer to the solution, how I fixed it. This is basically not a technological or programming error. Everything was correct here except the GOOGLE_CLOUD_STORAGE_BUCKET in .env file.

In my workplace, GCP is controlled by our Head of Engineering, who is our Lead DevOp as well. I asked him to create a storage repo folder for me to upload images via our application and give me service account credentials. He created and give me the folder name inside the bucket.

I used the folder name instead of bucket name in the .env file. When I show him the code, then he identified that it was wrong bucket name. I've updated the bucket name and everything is working fine.

Moral:

If nothing works, please check the communication with everyone related to the issue.