@saws/file-storage
v1.0.11
Published
<div align='center'>
Downloads
1
Readme
FileStorage
Service and library file storage operations.
Table of Contents
Installation
From the command line run:
npm install @saws/file-storage
Then add the included service (FileStorageService
) to your saws.js
file.
Development
In development, FileStorageService
will stand up Minio in a local docker container which has an S3 compatible API. In addition, it will create a bucket for your service.
Deployment
When you deploy a FileStorageService
, 1 resource will be stood up and configured for you:
- S3 Bucket
Services
@saws/file-storage
only includes one service: FileStorageService
FileStorageService
You can require the FileStorageService
and use it in your saws.js
file like so:
const { FileStorageService } = require('@saws/file-storage/file-storage-service')
// will almost exclusively be used as a dependency to another service
const files = new FileSorageService({
name: 'my-files',
})
The FileStorageService
constructor accepts the following options:
name: string
The name of your service. This should be unique across all of your services.
dependencies: ServiceDefinition[]
An array of all of the other services this service depends on. This will ensure that permissions, environment variables, and execution order are all set up.
When used as a dependency
When a FileStorageService
is used as a dependency to other services, and you are running in development, it will automatically attach (where applicable) the following environment variables into the dependent services:
S3_ENDPOINT: string
- The url to the locally running Minio server.S3_ACCESS_KEY: string
- The S3 access key to access the locally running Minio server.S3_SECRET_KEY: string
- The S3 secret key to access the locally running Minio server.
Libraries
@saws/file-storage
includes a library to help with file management.
FileStorage
The FileStorage
class contains methods for managing files in S3 with path based file/folder structures.
Example usage:
import { FileStorage } from '@saws/file-storage/file-storage-library'
const client = new FileStorage('my-file-storage-service-name')
In order for the FileStorage
class to work in a service, that service must list your FileStorageService
as a dependency.
getFile(path: string): Promise<GetObjectCommandOutput>
Retrieves the file located at the specified path from the S3 bucket. Returns a promise with the file content.
getFileUrl(path: string): Promise<string>
Generates and returns a presigned URL for directly accessing the specified file in the S3 bucket.
getFileUploadUrl(path: string): Promise<string>
Generates and returns a presigned URL for uploading a file to the specified path in the S3 bucket.
writeFile(path: string, file: Uint8Array): Promise<void>
Uploads the provided file to the specified path in the S3 bucket. Returns a promise that resolves upon successful upload.
deleteFile(path: string): Promise<void>
Deletes the file located at the specified path from the S3 bucket. Returns a promise that resolves upon successful deletion.
listFiles(path: string): Promise<S3.ObjectList>
Lists all files under the specified path in the S3 bucket. Returns a promise with an array of objects representing the files.