gatsby-source-gcs-image
gatsby-source-gcs-image
What Is This?
gatsby-source-gcs-image is a GatsbyJS source plugin that converts images from Google Cloud Storage into GatsbyJS File nodes--much like how this plugin works with AWS S3. See here for more.
The created GatsbyJS File nodes can then be manipulated with image processors like sharp.
Usage
Install
npm install --save-dev gatsby-source-gcs-image
or
yarn add -D gatsby-source-gcs-image
Configure
At a minimum GCP creds must be provided with either credsJson or pathToCreds. Some example configurations are:
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: 'gatsby-source-gcs-image',
options: {
bucketName: 'my-gcs-bucket.appspot.com',
bucketPath: '/path/to/images',
concurrency: 10,
pathToCreds: `${__dirname}/.gcp-creds.json`,
projectId: 'my-gcp-project',
},
},
// ...image transform plugins
],
}
or
// gatsby-config.js
module.exports = {
plugins: [
{
resolve: 'gatsby-source-gcs-image',
options: {
credsJson: process.env.ENV_VAR_WITH_CREDS,
},
},
// ...image transform plugins
],
}
Fields
| name | type | default | required | description |
|---|---|---|---|---|
| bucketName | string | * | GCS bucket (may exist in creds) | |
| bucketPath | string | prefix to source images from | ||
| concurrency | number | 10 | how many objects to source at once | |
| credsJson | string | * | JSON string with creds (optional if pathToCreds is provided) |
|
| pathToCreds | string | * | JSON file with creds (optional if credsJson is provided) |
|
| projectId | string | * | GCP project id (may exist in creds) |
Caching
Images are cached on their GCS object generation, so an object is only re-downloaded when its contents actually change. Unchanged objects are skipped entirely on subsequent builds — no signing, no download.
Signed urls are used only as a build-time transport and never reach the built site, which embeds static, transformed copies of every image. Their lifetime is therefore an internal detail rather than a configurable option.
Upgrading from 1.x
2.0.0 replaces clock-based caching with generation-based caching. Two breaking changes:
- the
expiresoption has been removed — url lifetime is no longer configurable - the
expiresAtfield has been removed from theGcsImagenode
gcsPath and generation are now exposed on the node. Nothing changes for queries that use name
and childImageSharp.
Previously, an expiring url forced a full re-download of every object and rotated every underlying
File node id once per expiry window. Any expires value below 10 minutes silently disabled caching
altogether.
Query Data
export const pageQuery = graphql`
{
allGcsImage {
edges {
node {
name
childImageSharp {
gatsbyImageData(layout: CONSTRAINED, width: 800)
}
}
}
}
}
`