StorageStorage ServicesGoogle Cloud Storage

Google Cloud Storage

Last updated:

Google Cloud Storage

Overview

Google Cloud Storage is a unified object storage for developers and enterprises, from live data serving to data analytics/ML to data archiving. It integrates seamlessly with Visulima upload's image transformation capabilities.

Installation

Note: If you hit this error: "TypeError: Expected signal to be an instanceof AbortSignal" #784.

npm install @google-cloud/storage gaxios
yarn add @google-cloud/storage gaxios
pnpm add @google-cloud/storage gaxios

Usage

import { LRUCache } from "lru-cache";
import { GCStorage } from "@visulima/storage/provider/gcs";
import ImageTransformer from "@visulima/storage/transformer/image";

const storage = new GCStorage({
    bucket: "upload",
    projectId: "test",
    maxUploadSize: "1GB",
    logger: console,
    expiration: { maxAge: "1h", purgeInterval: "15min" },
});

// Initialize cache for transformed images
const cache = new LRUCache({
    max: 1000, // Maximum number of cached items
    ttl: 3600000, // 1 hour in milliseconds
});

// Initialize image transformer for on-demand transformations
const imageTransformer = new ImageTransformer(storage, {
    cache,
    maxImageSize: 10 * 1024 * 1024, // 10MB
    cacheTtl: 3600, // 1 hour
});

// Upload a file
const file = await storage.put(fileBuffer, { filename: "image.jpg" });

// Transform the uploaded image
const thumbnail = await imageTransformer.resize(file.id, {
    width: 300,
    height: 200,
    fit: "cover",
    quality: 80,
});

Image Transformations

Google Cloud Storage works seamlessly with Visulima upload's image transformation features:

// Resize images
const resized = await imageTransformer.resize("file-id", {
    width: 800,
    height: 600,
    fit: "cover",
});

// Convert formats
const webp = await imageTransformer.transform("file-id", {
    format: "webp",
    quality: 85,
});

// Crop images
const cropped = await imageTransformer.crop("file-id", {
    width: 400,
    height: 300,
    left: 100,
    top: 50,
});

Configuration

NameTypeDescriptionDefault
bucketstringThe name of the bucket to use
projectIdstringGoogle Cloud project ID
maxUploadSizestringMaximum upload size (e.g., "1GB", "10MB")
expirationobjectFile expiration configuration
loggerLoggerLogger instance

Performance Considerations

  1. Global CDN: GCS automatically serves files through Google's global CDN
  2. Caching: Enable transformation caching to reduce processing overhead
  3. Signed URLs: Use signed URLs for private files with transformations
  4. Lifecycle Policies: Configure automatic cleanup of temporary transformations

Signed URLs with Transformations

For private files, combine signed URLs with transformations:

// Generate a signed URL for a transformed image
const signedUrl = await storage.getSignedUrl(fileId, {
    expires: Date.now() + 3600000, // 1 hour
    transform: {
        width: 500,
        height: 500,
        fit: "cover",
        format: "webp",
    },
});
Support

Contribute to our work and keep us going

Community is the heart of open source. The success of our packages wouldn't be possible without the incredible contributions of users, testers, and developers who collaborate with us every day.Want to get involved? Here are some tips on how you can make a meaningful impact on our open source projects.

Ready to help us out?

Be sure to check out the package's contribution guidelines first. They'll walk you through the process on how to properly submit an issue or pull request to our repositories.

Submit a pull request

Found something to improve? Fork the repo, make your changes, and open a PR. We review every contribution and provide feedback to help you get merged.

Good first issues

Simple issues suited for people new to open source development, and often a good place to start working on a package.
View good first issues