craftcms / google-cloud

Google Cloud Storage volume type for Craft CMS.
https://plugins.craftcms.com/google-cloud
MIT License
11 stars 10 forks source link

Unable to upload a file #40

Closed JulieVelghe closed 2 months ago

JulieVelghe commented 2 months ago

Description

Hi all, we've made a website for a client, where we now need to be able to upload files to a google cloud storage via a form. My media system for audio files is setup like this: image I am using variables from my env file:

GOOGLE_APPLICATION_CREDENTIALS="D:/Julie/Repos/Ritcs/erasmushogeschool-brussel-fb3422db4ecf.json" GOOGLE_CLOUD_PROJECT_ID="erasmushogeschool-brussel" GOOGLE_CLOUD_URL="https://storage.googleapis.com/ritcs-folio" When I submit my form with an audio file, I get a filesystem error (see attached pdf) Filesystem Error – craft_errors_FsException.pdf I've checked on Google Cloud, and our service account has the role 'Storage Admin', which should have enough rights to everything.

Steps to reproduce

  1. setup a local craft site.
  2. setup a google cloud storage
  3. setup a file system that connects to the cloud storage
  4. upload a file via the cms, or alternativly, setup a formie form with a file upload and test that way.

Additional info

linear[bot] commented 2 months ago

PT-1994 Unable to upload a file

angrybrad commented 2 months ago

Context here: https://github.com/craftcms/cms/discussions/15284#discussioncomment-10137597

If you remove Craft/PHP from the equation and try to connect to Google Cloud storage using the same credentials in your D:/Julie/Repos/Ritcs/erasmushogeschool-brussel-fb3422db4ecf.json file via the CLI, are you able to connect, upload, or delete files from the CLI in that scenario?

JulieVelghe commented 2 months ago

@angrybrad I haven't tried via the cli yet. I did try to remove Craft from the equation by making a simple php script that tries to upload a file using the same json credential file, and that seems to work. image image

the setup for this test was quite simple: image And below the script I used:

<?php

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

// Authenticating with keyfile data.
$storageClient = new StorageClient([
    'keyFilePath' => __DIR__ . '/data/erasmushogeschool-brussel-fb3422db4ecf.json',
]);
$bucket = $storageClient->bucket('ritcs-folio');

if ($bucket->exists()) {
        echo 'Bucket exists!';
}else{
    echo 'Bucket not found';
}

$buckets = $storageClient->buckets();

foreach ($buckets as $bucket) {
    echo $bucket->name() . PHP_EOL;
}

// Upload a file to the bucket.

$sPath = __DIR__ . '/data/PaulineNeverSaidIWasAnAngel.mp3';
$objectName = "audio/PaulineNeverSaidIWasAnAngel.mp3";

$options = [
    'name' => $objectName
];

try {
    $bucket -> upload(
        fopen($sPath, 'r'),
        $options
    );
    echo "File was successfully uploaded/written to Google Cloud Storage.";
} catch (Exception $e) {
    echo "Could not upload/write the file to Google Storage. Reason: {$e->getMessage()}";
}

?>
JulieVelghe commented 2 months ago

@angrybrad I've just tried to use the cli. I have been able to use the json key file and upload the audio file: image image What I did get however on the cli, when I first tried to list the projects, was an error telling me I needed to enable the Cloud Resource Manager api. Is this api also needed for the craft plugin? Or are there any other api's that perhaps need to be enabled?

croxton commented 2 months ago

@JulieVelghe this guide is a little old now but may help with permissions: https://gist.github.com/croxton/77c610289b631998283efa637f66b9f3

In particular note that bucket access must be set to fine-grained (per-object) NOT uniform (per bucket) to work properly with the Craft Google Cloud plugin.

JulieVelghe commented 2 months ago

@croxton yes, that fine-grained access did fix it.

angrybrad commented 2 months ago

Thanks, all - I've added more details to the setup instructions https://github.com/craftcms/google-cloud/commit/f4e29a2e993df2a88d94fe24c2942eb47578b893