@hudoro/upload-file
v0.0.1-beta.33
Published
Backend bundling for express
Keywords
Readme
Hudoro Upload File
Hudoro Upload File is a package that helps the file upload process easily and efficiently. Supports various file types, size validation, and flexible file extensions. Suitable for use on backend and frontend that require practical file upload features.
Package instalation
Instal package using pnpm
pnpm add @hudoro/upload-fileInstal package using yarn
yarn add @hudoro/upload-fileInstal package using npm
npm i @hudoro/upload-fileConfiguration
Configure the Google Cloud Storage client with your credentials:
import { gcsConfig } from "@hudoro/upload-file";
import { Storage } from "@google-cloud/storage";
const storage = new Storage({
keyFilename: "serviceaccount.json",
projectId: "your-project-id",
});
export const gcs = gcsConfig({
bucketName: "your-bucket",
gcsUrl: "https://storage.googleapis.com",
gcsFolder: "your-folder",
storage,
});Usage
The package provides several functions for managing files in Google Cloud Storage:
Get File Size Retrieve the size of a file:
// getFileSize(path)
const size = await gcs.getFileSize("/documents/file.pdf");Upload File Upload a file to Google Cloud Storage:
// uploadFile(buffer, mimetype)
const file = req.file;
const uploadResult = await gcs.uploadFile(file.buffer, file.mimetype);Copy Object Copy a file from one location to another:
// copyObject(from, to)
const copyResult = await gcs.copyObject("/images/profile.png", "/users/images/profile.png");Remove Object Delete a file from storage:
// removeObject(path)
const removeResult = await gcs.removeObject("/images/profile.png");Get Public URL Generate a public URL for accessing a file:
// getPublicUrl(fileName)
const url = await gcs.getPublicUrl("/images/profile.png");Get File Buffer Retrieve a file as a buffer:
// getFileBuffer(fileName)
const buffer = await gcs.getFileBuffer("/images/profile.png");Get File Stream Get a readable stream of a file:
// getFileStream(fileName)
const stream = await gcs.getFileStream("/images/profile.png");Check if Object Exists Verify if a file exists in storage:
// isObjectExist(fileName)
const exists = await gcs.isObjectExist("/images/profile.png");Get File Metadata Retrieve metadata for a file:
// getFileMetadata(fileName)
const metadata = await gcs.getFileMetadata("/images/profile.png");Complete Example
javascriptimport { gcsConfig } from "@hudoro/upload-file";
import { Storage } from "@google-cloud/storage";
const storage = new Storage({
keyFilename: "serviceaccount.json",
projectId: "your-project-id",
});
export const gcs = gcsConfig({
bucketName: "your-bucket",
gcsUrl: "https://storage.googleapis.com",
gcsFolder: "your-folder",
storage,
});
async function handleFileOperations() {
try {
// Upload a file
const fileBuffer = Buffer.from("Hello, World!");
const uploadResult = await gcs.uploadFile(fileBuffer, "text/plain");
console.log("Uploaded:", uploadResult);
// Get file size
const size = await gcs.getFileSize("/documents/file.txt");
console.log("File size:", size);
// Generate public URL
const url = await gcs.getPublicUrl("/documents/file.txt");
console.log("Public URL:", url);
// Check if file exists
const exists = await gcs.isObjectExist("/documents/file.txt");
console.log("File exists:", exists);
// Copy file to another location
await gcs.copyObject("/documents/file.txt", "/archive/file.txt");
console.log("File copied successfully");
// Delete original file
await gcs.removeObject("/documents/file.txt");
console.log("File removed successfully");
} catch (error) {
console.error("Error:", error);
}
}Error Handling
All functions include proper error handling. It's recommended to use try/catch blocks when calling these functions to handle any potential errors.
Parameter for gcs config
Props that you can pass to GCS Config
| Prop Name | Value | required | | :------------------- | :------------------------------------------ | :------- | | storage | Storage | true | | bucketName | string | true | | gcsUrl | string | true | | gcsFolder | string | true |
