Skip to content

Commit

Permalink
Cleanup & update README
Browse files Browse the repository at this point in the history
  • Loading branch information
f2face committed May 30, 2024
2 parents 0fe55f7 + e0bdf3d commit e8e4de7
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 18 deletions.
58 changes: 50 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This is a wrapper of the AWS S3 client library, designed to provide a user-frien

> ⚠ This library is currently in development and is not yet ready for production use. It is subject to change and may contain bugs or other issues. Please use it at your own risk.
### Installation
## Installation

#### npm

Expand All @@ -30,27 +30,34 @@ pnpm install node-cloudflare-r2
> \
> Check the latest version number in the [release page](https://github.com/f2face/cloudflare-r2/releases).
### Example
## Examples

### Basic usage

```javascript
import { R2 } from 'node-cloudflare-r2';
import { createReadStream } from 'fs';

// Initialize R2
const r2 = new R2({
accountId: '<YOUR_ACCOUNT_ID>',
accessKeyId: '<YOUR_R2_ACCESS_KEY_ID>',
secretAccessKey: '<YOUR_R2_SECRET_ACCESS_KEY>',
});

// Initialize bucket instance
const bucket = r2.bucket('<BUCKET_NAME>');

// Set your bucket's public URL
// [Optional] Provide the public URL(s) of your bucket, if its public access is allowed.
bucket.provideBucketPublicUrl('https://pub-xxxxxxxxxxxxxxxxxxxxxxxxx.r2.dev');

console.log(await bucket.exists());
// true
// Check if the bucket exists
console.log(await bucket.exists()); // true
```

### Upload local file (simple)

// Upload local file
```javascript
const upload = await bucket.uploadFile('/path/to/file', 'destination_file_name.ext');
console.log(upload);
/*
Expand All @@ -62,8 +69,43 @@ console.log(upload);
versionId: '',
}
*/
```

### Generate signed URL with expiration time

// Upload file or stream
const uploadStream = await bucket.uploadStream(createReadStream('/path/to/file'), 'destination_file_name-stream.ext');
```javascript
// Generate signed link that expires after 3600 seconds.
const signedUrl = await bucket.getObjectSignedUrl('destination_file_name.ext', 3600);
console.log(signedUrl);
/*
https://bucket-name.cloudflare-account-id.r2.cloudflarestorage.com/destination_file_name.ext?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Credential=...&X-Amz-Date=...&X-Amz-Expires=60&X-Amz-Signature=...&X-Amz-SignedHeaders=host&x-id=GetObject
*/
```

### Upload string or binary data or stream

```javascript
// Upload text content
const content = 'Lorem ipsum';
const uploadContent = await bucket.upload(content, 'lorem-ipsum.txt');
```

```javascript
import { createReadStream } from 'fs';

// Upload from fs.createReadStream()
const stream = createReadStream('/path/to/file');
const uploadStream = await bucket.upload(stream, 'destination_file_name2.ext');
```

### Upload stream (advanced)

This `bucket.uploadStream()` method allows uploading big file or piping stream or stdout directly to your bucket (using multipart upload internally).

```javascript
// Let's say, you want to record a live stream and pipe it directly to your bucket.
import { spawn } from 'child_process';

const streamlink = spawn('streamlink', ['--stdout', '<LIVE_STREAM_HLS_URL>', 'best']);
const uploadLiveStreamVideo = await bucket.uploadStream(streamlink.stdout, 'my_live_stream.ts');
```
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-cloudflare-r2",
"version": "0.2.0",
"version": "0.2.1",
"description": "S3 wrapper for Cloudflare R2.",
"main": "./lib/index.js",
"scripts": {
Expand Down Expand Up @@ -58,4 +58,3 @@
"typescript": "^5.4.2"
}
}

9 changes: 1 addition & 8 deletions src/Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,14 +353,7 @@ export class Bucket {
* @deprecated
*/
public async deleteFile(file: string) {
const result = await this.r2.send(
new DeleteObjectCommand({
Bucket: this.name,
Key: file,
})
);

return result.$metadata.httpStatusCode === 200;
return this.deleteObject(file);
}

/**
Expand Down

0 comments on commit e8e4de7

Please sign in to comment.