diff --git a/README.md b/README.md index b88a4f9..2315b60 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: '', accessKeyId: '', secretAccessKey: '', }); +// Initialize bucket instance const bucket = r2.bucket(''); -// 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); /* @@ -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', '', 'best']); +const uploadLiveStreamVideo = await bucket.uploadStream(streamlink.stdout, 'my_live_stream.ts'); +``` diff --git a/package.json b/package.json index 7622f00..b384f6b 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -58,4 +58,3 @@ "typescript": "^5.4.2" } } - diff --git a/src/Bucket.ts b/src/Bucket.ts index ecdc6bf..dbb0705 100644 --- a/src/Bucket.ts +++ b/src/Bucket.ts @@ -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); } /**