Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions libs/bunny-storage/examples/deno/batch-upload/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Batch Upload Example

This example demonstrates how to upload an entire directory (including subdirectories) to Bunny Storage.

## Prerequisites

- [Deno](https://deno.land/) installed
- A Bunny Storage zone with API credentials

### Installing Deno

```bash
# macOS/Linux
curl -fsSL https://deno.land/install.sh | sh

# Windows (PowerShell)
irm https://deno.land/install.ps1 | iex
```

## Setup

Set the required environment variables:

```bash
export BUNNY_STORAGE_API_KEY="your-storage-api-key"
export BUNNY_STORAGE_ZONE="your-storage-zone-name"
export BUNNY_STORAGE_REGION="de" # Optional, defaults to "de" (Falkenstein)
```

## Running the Example

```bash
deno task start ./local-folder /remote-folder
```

Arguments:

- `local-directory` - The local directory to upload
- `remote-directory` - The destination directory in Bunny Storage (default: `/`)
8 changes: 8 additions & 0 deletions libs/bunny-storage/examples/deno/batch-upload/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tasks": {
"start": "deno run --allow-net --allow-env --allow-read index.ts"
},
"imports": {
"@bunny.net/storage-sdk": "npm:@bunny.net/storage-sdk@latest"
}
}
107 changes: 107 additions & 0 deletions libs/bunny-storage/examples/deno/batch-upload/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import * as BunnyStorage from "@bunny.net/storage-sdk";

const apiKey = Deno.env.get("BUNNY_STORAGE_API_KEY");
const zoneName = Deno.env.get("BUNNY_STORAGE_ZONE");
const region = Deno.env.get("BUNNY_STORAGE_REGION") || "de";

if (!apiKey || !zoneName) {
console.error("Missing required environment variables:");
console.error(" BUNNY_STORAGE_API_KEY - Your storage zone API key");
console.error(" BUNNY_STORAGE_ZONE - Your storage zone name");
Deno.exit(1);
}

const localDir = Deno.args[0];
const remoteDir = Deno.args[1] || "/";

if (!localDir) {
console.error(
"Usage: deno run --allow-net --allow-env --allow-read index.ts <local-directory> [remote-directory]",
);
console.error(
"Example: deno run --allow-net --allow-env --allow-read index.ts ./uploads /backups",
);
Deno.exit(1);
}

const storageRegion = region as BunnyStorage.regions.StorageRegion;
const storageZone = BunnyStorage.zone.connect_with_accesskey(
storageRegion,
zoneName,
apiKey,
);

const results: {
localPath: string;
remotePath: string;
size: number;
success: boolean;
error?: string;
}[] = [];

async function uploadDirectory(localPath: string, remotePath: string) {
for await (const entry of Deno.readDir(localPath)) {
const entryLocalPath = `${localPath}/${entry.name}`;
const entryRemotePath = `${remotePath}/${entry.name}`;

if (entry.isDirectory) {
await uploadDirectory(entryLocalPath, entryRemotePath);
} else if (entry.isFile) {
try {
const file = await Deno.open(entryLocalPath, { read: true });
const fileInfo = await file.stat();
const stream = file.readable;

const success = await BunnyStorage.file.upload(
storageZone,
entryRemotePath,
stream,
);

results.push({
localPath: entryLocalPath,
remotePath: entryRemotePath,
size: fileInfo.size,
success,
});
} catch (error) {
results.push({
localPath: entryLocalPath,
remotePath: entryRemotePath,
size: 0,
success: false,
error: error.message,
});
}
}
}
}

try {
await uploadDirectory(localDir, remoteDir);

console.log(
JSON.stringify(
{
storageZone: zoneName,
region,
localDirectory: localDir,
remoteDirectory: remoteDir,
results,
summary: {
total: results.length,
successful: results.filter((r) => r.success).length,
failed: results.filter((r) => !r.success).length,
totalSize: results
.filter((r) => r.success)
.reduce((sum, r) => sum + r.size, 0),
},
},
null,
2,
),
);
} catch (error) {
console.error("Error:", error.message);
Deno.exit(1);
}
34 changes: 34 additions & 0 deletions libs/bunny-storage/examples/deno/delete-file/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Delete File Example

This example demonstrates how to delete a single file from Bunny Storage.

## Prerequisites

- [Deno](https://deno.land/) installed
- A Bunny Storage zone with API credentials

### Installing Deno

```bash
# macOS/Linux
curl -fsSL https://deno.land/install.sh | sh

# Windows (PowerShell)
irm https://deno.land/install.ps1 | iex
```

## Setup

Set the required environment variables:

```bash
export BUNNY_STORAGE_API_KEY="your-storage-api-key"
export BUNNY_STORAGE_ZONE="your-storage-zone-name"
export BUNNY_STORAGE_REGION="de" # Optional, defaults to "de" (Falkenstein)
```

## Running the Example

```bash
deno task start /path/to/file.txt
```
8 changes: 8 additions & 0 deletions libs/bunny-storage/examples/deno/delete-file/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tasks": {
"start": "deno run --allow-net --allow-env index.ts"
},
"imports": {
"@bunny.net/storage-sdk": "npm:@bunny.net/storage-sdk@latest"
}
}
49 changes: 49 additions & 0 deletions libs/bunny-storage/examples/deno/delete-file/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as BunnyStorage from "@bunny.net/storage-sdk";

const apiKey = Deno.env.get("BUNNY_STORAGE_API_KEY");
const zoneName = Deno.env.get("BUNNY_STORAGE_ZONE");
const region = Deno.env.get("BUNNY_STORAGE_REGION") || "de";

if (!apiKey || !zoneName) {
console.error("Missing required environment variables:");
console.error(" BUNNY_STORAGE_API_KEY - Your storage zone API key");
console.error(" BUNNY_STORAGE_ZONE - Your storage zone name");
Deno.exit(1);
}

const path = Deno.args[0];

if (!path) {
console.error("Usage: deno run --allow-net --allow-env index.ts <path>");
console.error(
"Example: deno run --allow-net --allow-env index.ts /folder/file.txt",
);
Deno.exit(1);
}

const storageRegion = region as BunnyStorage.regions.StorageRegion;
const storageZone = BunnyStorage.zone.connect_with_accesskey(
storageRegion,
zoneName,
apiKey,
);

try {
const success = await BunnyStorage.file.remove(storageZone, path);

console.log(
JSON.stringify(
{
storageZone: zoneName,
region,
deleted: path,
success,
},
null,
2,
),
);
} catch (error) {
console.error("Error deleting file:", error.message);
Deno.exit(1);
}
34 changes: 34 additions & 0 deletions libs/bunny-storage/examples/deno/delete-multiple-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Delete Multiple Files Example

This example demonstrates how to delete multiple files from Bunny Storage in a single operation.

## Prerequisites

- [Deno](https://deno.land/) installed
- A Bunny Storage zone with API credentials

### Installing Deno

```bash
# macOS/Linux
curl -fsSL https://deno.land/install.sh | sh

# Windows (PowerShell)
irm https://deno.land/install.ps1 | iex
```

## Setup

Set the required environment variables:

```bash
export BUNNY_STORAGE_API_KEY="your-storage-api-key"
export BUNNY_STORAGE_ZONE="your-storage-zone-name"
export BUNNY_STORAGE_REGION="de" # Optional, defaults to "de" (Falkenstein)
```

## Running the Example

```bash
deno task start /file1.txt /file2.txt /folder/file3.txt
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"tasks": {
"start": "deno run --allow-net --allow-env index.ts"
},
"imports": {
"@bunny.net/storage-sdk": "npm:@bunny.net/storage-sdk@latest"
}
}
59 changes: 59 additions & 0 deletions libs/bunny-storage/examples/deno/delete-multiple-files/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import * as BunnyStorage from "@bunny.net/storage-sdk";

const apiKey = Deno.env.get("BUNNY_STORAGE_API_KEY");
const zoneName = Deno.env.get("BUNNY_STORAGE_ZONE");
const region = Deno.env.get("BUNNY_STORAGE_REGION") || "de";

if (!apiKey || !zoneName) {
console.error("Missing required environment variables:");
console.error(" BUNNY_STORAGE_API_KEY - Your storage zone API key");
console.error(" BUNNY_STORAGE_ZONE - Your storage zone name");
Deno.exit(1);
}

const paths = Deno.args;

if (paths.length === 0) {
console.error(
"Usage: deno run --allow-net --allow-env index.ts <path1> <path2> ...",
);
console.error(
"Example: deno run --allow-net --allow-env index.ts /file1.txt /file2.txt /folder/file3.txt",
);
Deno.exit(1);
}

const storageRegion = region as BunnyStorage.regions.StorageRegion;
const storageZone = BunnyStorage.zone.connect_with_accesskey(
storageRegion,
zoneName,
apiKey,
);

const results: { path: string; success: boolean; error?: string }[] = [];

for (const path of paths) {
try {
const success = await BunnyStorage.file.remove(storageZone, path);
results.push({ path, success });
} catch (error) {
results.push({ path, success: false, error: error.message });
}
}

console.log(
JSON.stringify(
{
storageZone: zoneName,
region,
results,
summary: {
total: results.length,
successful: results.filter((r) => r.success).length,
failed: results.filter((r) => !r.success).length,
},
},
null,
2,
),
);
39 changes: 39 additions & 0 deletions libs/bunny-storage/examples/deno/delete-old-files/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Delete Old Files Example

This example demonstrates how to delete files older than a specified number of days from a directory in Bunny Storage.

## Prerequisites

- [Deno](https://deno.land/) installed
- A Bunny Storage zone with API credentials

### Installing Deno

```bash
# macOS/Linux
curl -fsSL https://deno.land/install.sh | sh

# Windows (PowerShell)
irm https://deno.land/install.ps1 | iex
```

## Setup

Set the required environment variables:

```bash
export BUNNY_STORAGE_API_KEY="your-storage-api-key"
export BUNNY_STORAGE_ZONE="your-storage-zone-name"
export BUNNY_STORAGE_REGION="de" # Optional, defaults to "de" (Falkenstein)
```

## Running the Example

```bash
deno task start /backups 30
```

Arguments:

- `directory` - The directory to scan for old files
- `days` - Delete files older than this many days (default: 30)
Loading