1.0.2 • Published 5 months ago
@e3o/gcp-storage v1.0.2
storage
This modules provides methods manage object storage in Google Cloud Storage.
How to use
Import
import { FileStorage } from 'storage'
Init
First, we need an instance of FileStorage
export const fileStorage = new FileStorage({
bucket: 'BUCKET_NAME',
keyFilename: 'STORAGE_KEY_FILENAME',
})
STORAGE_KEY_FILENAME
is the credential file URI
Upload file
Upload a file to the remote storage.
const fileName = await fileStorage.upload(
{
pathDestination: 'path',
fileName: 'testFile',
file: Buffer.from('Test content', 'utf-8'),
contentType: "text/plain"
metadata: { something: 'here' }
}
)
// fileName -> path/testFile
// OR
const fileName = await fileStorage.send(
{
pathDestination: 'path',
fileName: 'testFile',
file: fs.createReadStream('./test.png'),
contentType: "img/png"
metadata: { something: 'here' }
}
)
// fileName -> path/testFile
- The
file
attribute MUST be aBuffer
or aReadableStream
. - The
contentType
attribute is not required default to "application/octet-stream" - The
metadata
object is not required. Values are converted tostring
Download file
Download a file from the bucket
const fileBuffer = await fileStorage.download(fileName)
fileName
is the string returned by theupload()
method
Misc
getMetadata
Get the global metadata of an object
const metadata = await fileStorage.getMetadata(fileName)
/*
metadata ->
{ kind: 'storage#object',
id: 'sandbox',
selfLink:
'https://www.googleapis.com/storage/v1/b/BUCKET_NAME/o/path%2FtestFile',
name: 'path/testFile',
bucket: 'BUCKET_NAME',
generation: '123456789',
metageneration: '1',
contentType: 'application/octet-stream',
storageClass: 'STANDARD',
size: '12',
md5Hash: 'MD5_HASH',
mediaLink:
'https://storage.googleapis.com/download/storage/v1/b/BUCKET_NAME/o/path%2FtestFile?generation=123456789&alt=media',
cacheControl: 'public, max-age=31536000',
crc32c: 'r5b9xg==',
etag: 'CJD+6K2xsuUCEAE=',
timeCreated: '2019-10-23T12:30:40.811Z',
updated: '2019-10-23T12:30:40.811Z',
timeStorageClassUpdated: '2019-10-23T12:30:40.811Z'
metadata: { something: 'here', type: 'text' }
}
*/
fileName
is the string returned by theupload()
method