Created
August 16, 2023 16:05
-
-
Save duanebester/7ddf4779bb6cd956dc54d65a3fb98a31 to your computer and use it in GitHub Desktop.
Example of how to save files in a db and gcs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { GetSignedUrlConfig, Storage, File, Bucket } from '@google-cloud/storage'; | |
const storage = new Storage({ | |
keyFilename: './config/creds.json', | |
}); | |
type UploadedFile = { | |
url: string; | |
bucketId: string; | |
bucketName: string; | |
bucketPath: string; | |
fileName: string; | |
}; | |
const uploadFileToBucket = ( | |
bucket: Bucket, | |
file: Express.Multer.File, | |
path: string = 'default-path' | |
) => | |
new Promise<UploadedFile>((resolve, reject) => { | |
const { originalname, buffer } = file; | |
const blob: File = bucket.file(path); | |
const blobStream = blob.createWriteStream({ | |
resumable: false, | |
}); | |
blobStream | |
.on('finish', () => { | |
const url = `https://storage.googleapis.com/${bucket.name}/${blob.name}`; | |
const bucketId = bucket.id; | |
const bucketName = bucket.name; | |
const bucketPath = blob.name; | |
resolve({ url, bucketId, bucketName, bucketPath, fileName: originalname }); | |
}) | |
.on('error', (err) => { | |
reject(`Unable to upload file, something went wrong: ${err}`); | |
}) | |
.end(buffer); | |
}); | |
// Upload file api endpoint | |
(async () => { | |
// the request comes in with file | |
let file: Express.Multer.File; | |
// somehow we get the organization id and/or user id from the request | |
const userId = 'some-user-id'; | |
const organizationId = 'some-organization-id'; | |
// save a file to gcs | |
const bucket = storage.bucket('my-bucket'); | |
const path = `${organizationId}/${userId}`; | |
const { url, bucketId, bucketName, bucketPath, fileName } = await uploadFileToBucket( | |
bucket, | |
file, | |
path | |
); | |
const savedFileInfo = { | |
organizationId, | |
userId, | |
url, | |
bucketId, | |
bucketName, | |
bucketPath, | |
fileName, | |
}; | |
// save the GCS file info to db | |
// return db.collection.save('uploaded-files', savedFileInfo) | |
})(); | |
// List files owned by organization api endpoint | |
(async () => { | |
// somehow we get the organization id | |
const organizationId = 'some-organization-id'; | |
// get all files from db | |
// const files = await db.collection.find('uploaded-files', { organizationId }); | |
// return files | |
})(); | |
// Download file api endpoint | |
(async () => { | |
// somehow we get the organization id from request | |
const organizationId = 'some-organization-id'; | |
// somehow we get file id from request | |
const fileId = 'some-file-id'; | |
// get file from db | |
// const file = await db.collection.findOne('uploaded-files', { organizationId, fileId }); | |
// dummy file "from db" for example to work... | |
const file = { bucketName: 'my-bucket', bucketPath: 'some-path' }; | |
// get signed url | |
const bucket = storage.bucket(file.bucketName); | |
const fileToDownload = bucket.file(file.bucketPath); | |
const config: GetSignedUrlConfig = { | |
action: 'read', | |
expires: Date.now() + 1000 * 60 * 60, // 1 hour | |
}; | |
const [url] = await fileToDownload.getSignedUrl(config); | |
// we return the url to the client for them to click and download | |
return url; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment