#Error when implementing plugin-cloud-storage
29 messages · Page 1 of 1 (latest)
Hi @pastel aspen - does it work if you prefix your environment variable with PUBLIC_PAYLOAD_ ? See docs here https://payloadcms.com/docs/admin/webpack#admin-environment-vars
based on the Payload Cloud Storage Plugin documentation, when using GCS Adapter I don't see that they prefix PUBLIC_PAYLOAD_ with the environment variables: https://github.com/payloadcms/plugin-cloud-storage#gcs-adapter
What do you get when you console.log JSON.parse(process.env.GCS_CREDENTIALS) ?
Through console.log I get the following error: SyntaxError: Unexpected end of JSON input at JSON.parse (<anonymous>).
My credentials are defined as follows:
GCS_CREDENTIALS =. { "type": "xxx", "project_id": "xxx", "private_key_id": "xx", "private_key": "xxx", "client_email": "xxx", "client_id": "xxx", "auth_uri": "xxx", "token_uri": "xxx", "auth_provider_x509_cert_url": "xxxx", "client_x509_cert_url": "xxx". }
The error occurs when parsing the JSON: JSON.parse(process.env.GCS_CREDENTIALS)
So if you console.log(process.env.GCS_CREDENTIALS) does it return the above object?
If that is the exact object you're using, this error could be due to invalid json, you have a period on the end of the last line here "client_x509_cert_url": "xxx". which should be removed
There is no period, not if I have correctly declared my credentials in JSON object as variable environment.
Is that screenshot directly from your .env? If so, you need to put the entire object on one line
ok, but in JSON format? Because the plugin plugin-cloud-storage plugin parse a JSON: import { gcsAdapter } from '@payloadcms/plugin-cloud-storage/gcs'; const adapter = gcsAdapter({ options: { // you can choose any method for authentication, and authorization which is being provided by@google-cloud/storage`
keyFilename: './gcs-credentials.json',
//OR
credentials: JSON.parse(process.env.GCS_CREDENTIALS) // this env variable will have stringify version of your credentials.json file
},
bucket: process.env.GCS_BUCKET,
})
// Now you can pass this adapter to the plugin`
Yes, so it looks like this screenshot.
In the terminal I am console logging twice, first box is **process.env.CREDS **then JSON.parse(process.env.CREDS) and you can see it come through as expected
now I get the following error 😢
@pastel aspen have you run yarn in your project?
yes, I use yarn to compile my project
@pastel aspen the errors look to be coming from other dependencies now
it seems to require stream-browserify to be able to process the JSON.
I've run into the same problem. @pastel aspen have you managed to solve this?
Hi, yes I got it, the credentials must be declared as public and in one line:PAYLOAD_PUBLIC_GCS_CREDENTIALS ={"type": "xx",....} and PAYLOAD_PUBLIC_GCS_BUCKET=xxx, also, you must parse it in the following way, credentials: JSON.parse(process.env.PAYLOAD_PUBLIC_GCS_CREDENTIALS.replace(/\n/g, ''))
and this will solve these errors? #1087231526686703726 message
Yes, and I am using the following versions:
"@google-cloud/storage":"^6.9.4", "@payloadcms/plugin-cloud-storage": "^1.0.14", "payload": "^1.6.26".
got it, thanks!
Hi @mystic palm, case update, I had problems when deploying my project, the error occurred when reading: credentials: JSON.parse(process.env.PAYLOAD_PUBLIC_GCS_CREDENTIALS.replace(/\n/g, ''))
The solution is to read the json file directly: keyFilename: path.resolve(__dirname, "./gcs-credentials.json")
I’ve managed to get it working with default code from the readme, the problem specifically about the gcs creds was within the stringified version of my service account key. Locally the key string stored in the .env file had to be wrapped in single quotes, but when deploying and using cloud run variables I had to remove the quotes and all worked well
I wouldn’t advise keeping the gcs creds inside the repo @pastel aspen , better use cloud run env variables, that way anyone with access to your repo/code will not get access to your gc services
@mystic palm I understand, and how did you manage to read the .env variables? I'm doing it this way locally and I'm getting an error: credentials: JSON.parse(process.env.PAYLOAD_PUBLIC_GCS_CREDENTIALS)
are you asking how I managed to read it locally?
if you’re asking how I read it on the cloud run deployment, in cloud run go into your service, click “edit revision” and there you can add all your env variables
Cloud run -> YOUR SERVICE -> Edit & deploy new revision -> Environment variables
exactly, how did you do to read them locally?
I didn’t change anything with regards to the reading locally. Default .env file with all the variables (without the PUBLIC_ prefix).