Google Cloud Storage
Installation
Install package
npm i @uplo/service-gcsAdd service to Uplo
import { createGCSService } from '@uplo/service-gcs'
const uplo = createUplo({
services: {
google: createGCSService({
isPublic: false,
credentialsPath: path.resolve(__dirname, './config/gcp-credentials.json'),
bucket: process.env.GCS_BUCKET,
}),
},
})Authentication
The service forwards auth options to @google-cloud/storage. Pick one:
Key file path
createGCSService({
bucket: process.env.GCS_BUCKET,
credentialsPath: '/path/to/gcp-credentials.json',
})Inline credentials
Useful when the service account JSON lives in a secret manager or env var.
createGCSService({
bucket: process.env.GCS_BUCKET,
credentials: {
client_email: process.env.GCP_CLIENT_EMAIL,
private_key: process.env.GCP_PRIVATE_KEY,
},
projectId: process.env.GCP_PROJECT_ID,
})Application Default Credentials (ADC)
Pass no credentials. The SDK auto-discovers from the GOOGLE_APPLICATION_CREDENTIALS env var, gcloud user creds, or the workload’s attached service account (Cloud Run, GKE, GCE, Cloud Functions).
createGCSService({
bucket: process.env.GCS_BUCKET,
})Direct Upload
CORS
This is an example CORS configuration for direct uploads. To setup CORS, read a guide in Google Cloud Docs .
[
{
"origin": ["https://www.example.com"],
"method": ["PUT"],
"responseHeader": ["Origin", "Content-Type", "Content-MD5", "Content-Disposition"],
"maxAgeSeconds": 3600
}
]Last updated on