https://github.com/cyclic-software/s3fs
Drop in replacement for the Node.js fs library backed by AWS S3
https://github.com/cyclic-software/s3fs
aws cyclic-sh fs nodejs s3
Last synced: 11 months ago
JSON representation
Drop in replacement for the Node.js fs library backed by AWS S3
- Host: GitHub
- URL: https://github.com/cyclic-software/s3fs
- Owner: cyclic-software
- License: gpl-3.0
- Created: 2022-12-09T22:37:50.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-10-18T20:18:21.000Z (over 2 years ago)
- Last Synced: 2024-11-11T21:20:42.775Z (over 1 year ago)
- Topics: aws, cyclic-sh, fs, nodejs, s3
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/@cyclic.sh/s3fs
- Size: 1010 KB
- Stars: 123
- Watchers: 3
- Forks: 8
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @cyclic.sh/s3fs
Drop in replacement for the Node.js `fs` library backed by AWS S3.
Use the same methods as `fs` and enjoy the convenience of S3 buckets without the hassle of AWS.
[](https://discord.cyclic.sh/support) [](https://github.com/cyclic-software/s3fs/actions/workflows/run_tests.yaml) [](https://github.com/semantic-release/semantic-release)
[](https://www.npmjs.com/package/@cyclic.sh/s3fs)   [](https://snyk.io/advisor/npm-package/@cyclic.sh/s3fs)
## Supported methods
`@cyclic.sh/s3fs` supports the following `fs` methods operating on AWS S3:
- writeFile / writeFileSync
- readFile / readFileSync
- exists / existsSync
- rm / rmSync
- stat / statSync
- unlink / unlinkSync
- readdir / readdirSync
- mkdir / mkdirSync
- rmdir / rmdirSync
## Example Usage
### Installation
```
npm install @cyclic.sh/s3fs
```
Require in the same format as Node.js `fs`, specifying an S3 Bucket:
- Callbacks and Sync methods:
```js
const fs = require('@cyclic.sh/s3fs')(S3_BUCKET_NAME)
```
- Promises (for async/await functions):
```js
const fs = require('@cyclic.sh/s3fs/promises')(S3_BUCKET_NAME)
```
- On Cyclic.sh
- Alternatively, when using with cyclic.sh or if the environment variable `CYCLIC_BUCKET_NAME` is set to an S3 bucket name, initialization can happen without specifying a bucket:
```js
const fs = require('@cyclic.sh/s3fs')
```
or
```js
const fs = require('@cyclic.sh/s3fs/promises')
```
### Authentication
Authenticating the client:
- **cyclic.sh** -
- When deploying on cyclic.sh, credentials are already available in the environment.
- The bucket name is also available under the `CYCLIC_BUCKET_NAME` variable.
- Read more: Cyclic Environment Variables
- **Local Mode** - When no credentials are available, the client will fall back to using `fs` and the local filesystem. A warning will show in the terminal.
- **Environment Variables** - The internal S3 client will use AWS credentials if they are set in the local environment. Variables provided by Cyclic.sh may be found in "Data/Storage" tab in the user's Cyclic dashboard and will reset after 60 minutes.
```
AWS_REGION
AWS_ACCESS_KEY_ID
AWS_SECRET_KEY
AWS_SECRET_ACCESS_KEY
```
- **Client Credentials** - The library also accepts standard S3 client parameters at initialization. For example:
```js
const fs = require('@cyclic.sh/s3fs')(S3_BUCKET_NAME, {
region: ...
credentials: {...}
})
```
### Using Methods
The supported methods have the same API as Node.js `fs` and can be written as follows:
- Sync
```js
const fs = require('@cyclic.sh/s3fs')(S3_BUCKET_NAME)
const json = JSON.parse(fs.readFileSync('test/_read.json'))
```
- Callbacks
```js
const fs = require('@cyclic.sh/s3fs')(S3_BUCKET_NAME)
fs.readFile('test/_read.json', (error,data)=>{
const json = JSON.parse(data)
})
```
- Promises (async/await)
```js
const fs = require('@cyclic.sh/s3fs/promises')(S3_BUCKET_NAME)
async function run(){
const json = JSON.parse(await fs.readFile('test/_read.json'))
}
```