The GCP Bucket Library facilitates seamless interactions with Google Cloud Storage buckets. It allows the uploading of files from various data types like strings, blobs, or buffers. Additionally, if the file to be uploaded is an image, the library provides the capability to resize the image into multiple dimensions as specified.
First, you need to initialize an instance of the GCPBucket
class.
// Initialize your GCPBucket instance
const gcpBucket = new GCPBucket({
bucketName: 'your-bucket-name',
firebaseApp: <your firebase-admin instance>,
chunkSize: 1024, // Optional chuck size to upload
encryptKey: "key" // Optional An AES-256 encryption key.
});
// Define the folder and file name for the upload
const folderName = 'uploads';
const fileName = file.originalname;
// Prepare file content
const fileContent = {
folderName,
fileName,
fileData: file.buffer,
};
// Upsert the file to GCP
const result = await gcpBucket.upsertFiles(fileContent);
// The result will be
// [
// {
// "fileUrl": "file-url",
// "filePath": "file-path",
// "fileName": "file-name",
// "fileType": "file-type",
// "fileContentType": "file-content-type"
// }
// ]
// Define the folder and file name for the upload
const folderName = 'uploads';
const fileName = file.originalname;
// Prepare file content
const fileContent = {
folderName,
fileName,
fileData: file.buffer,
resizeOptions: [
{
height: 400,
width: 400,
fileName: "MediumImgFile"
fit: 'inside',
},
{ height: 200, width: 200, fileResizePrefix: 'small-', fit: 'inside' },
],
};
// Upsert the file to GCP
const result = await gcpBucket.upsertFiles(fileContent, callbackProcessPercentage);
callbackProcessPercentage is a callback function that gets invoked with the file path and the percentage of the resize process.
Deleting a File
-
filePath
(string
) : The file path in the bucket.
await gcpBucket.deleteFile(filePath);
Downloading a File
-
filePath
(string
) : The file path in the bucket.
await gcpBucket.download(filePath);
Checking if a File is an Image
-
fileData
(TFileContent['fileData']
) : The image data which can be a Base64 string, Blob, or Buffer.
await gcpBucket.isImage(fileData);
This method resizes an image by a specified scale factor and returns the new dimensions along with a buffer of the resized image.
-
data
(TFileContent['fileData']
): The image data which can be a Base64 string, Blob, or Buffer. -
scaleFactor
(number
): The scaling factor to resize the image. For example, a scaleFactor of 0.5 will reduce the image dimensions by 50%. -
imgFit
(keyof sharp.FitEnum
| optional): The fit strategy for sharp to follow when resizing the image. Default is undefined.
An object containing:
-
width
(number
): The new width of the resized image. -
height
(number
): The new height of the resized image. -
buffer
(Buffer
): A buffer of the resized image.
const result = await gcpBucket.getImageSizeByFactor(fileData, 0.5, 'inside');
This method retrieves and returns metadata of the specified image.
-
data
(TFileContent['fileData']
): The image data which can be a Base64 string, Blob, or Buffer.
A Promise resolving to a sharp.Metadata
object containing metadata information of the image.
const metadata = await gcpBucket.getImageMetadata(fileData);
This method return a new image with other size.
-
data
(TFileContent['fileData']
): The image data which can be a Base64 string, Blob, or Buffer. -
newSize
(TResizeOptions
): The new image dimensions.
-
buffer
(Buffer
): A buffer of the resized image.
const metadata = await gcpBucket.getImage(fileData, {
height: 100,
width: 100,
fit: 'cover',
});