Skip to content

Commit

Permalink
Merge pull request #40 from Aragur/main
Browse files Browse the repository at this point in the history
⚡ Add a Deno example to wipe an Appwrite bucket
  • Loading branch information
christyjacob4 committed Dec 20, 2022
2 parents d705ca6 + a6b6860 commit 579db4f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
41 changes: 41 additions & 0 deletions deno/wipe_appwrite_bucket/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# 🗑 Wipe Appwrite Bucket

A Deno cloud function to wipe a complete bucket in Appwrite by passing the bucket id as a payload.

_Example input:_

```json
{
"bucketId":"profilePictures"
}
```

_Example output:_

```json
{
"success":true
}
```

## 📝 Environment Variables

List of environment variables used by this cloud function:

- **APPWRITE_FUNCTION_ENDPOINT** - Endpoint of Appwrite project
- **APPWRITE_FUNCTION_API_KEY** - Appwrite API Key
- **APPWRITE_FUNCTION_PROJECT_ID** - Appwrite project ID. If running on Appwrite, this variable is provided automatically.

## 🚀 Deployment

There are two ways of deploying the Appwrite function, both having the same results, but each using a different process. We highly recommend using CLI deployment to achieve the best experience.

### Using CLI

Make sure you have [Appwrite CLI](https://appwrite.io/docs/command-line#installation) installed, and you have successfully logged into your Appwrite server. To make sure Appwrite CLI is ready, you can use the command `appwrite client --debug` and it should respond with green text `✓ Success`.

Make sure you are in the same folder as your `appwrite.json` file and run `appwrite deploy function` to deploy your function. You will be prompted to select which functions you want to deploy.

### Manual using tar.gz

Manual deployment has no requirements and uses Appwrite Console to deploy the tag. First, enter the folder of your function. Then, create a tarball of the whole folder and gzip it. After creating `.tar.gz` file, visit Appwrite Console, click on the `Deploy Tag` button and switch to the `Manual` tab. There, set the `entrypoint` to `src/mod.ts`, and upload the file we just generated.
1 change: 1 addition & 0 deletions deno/wipe_appwrite_bucket/src/deps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * as sdk from "https://deno.land/x/appwrite@6.0.0/mod.ts";
50 changes: 50 additions & 0 deletions deno/wipe_appwrite_bucket/src/mod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { sdk } from "./deps.ts";

export default async function (req: any, res: any) {
if (
!req.variables["APPWRITE_FUNCTION_ENDPOINT"] ||
!req.variables["APPWRITE_FUNCTION_PROJECT_ID"] ||
!req.variables["APPWRITE_FUNCTION_API_KEY"] ||
!req.payload
) {
return res.json({
success: false,
message: "Missing required environment variables or payload.",
});
}
let bucketId = "";
try {
const payload = JSON.parse(req.payload);
bucketId = payload.bucketId;
} catch (_err) {
return res.json({
success: false,
message: "Payload is invalid.",
});
}
const client = new sdk.Client();
const storage = new sdk.Storage(client);

client
.setEndpoint(req.variables["APPWRITE_FUNCTION_ENDPOINT"])
.setProject(req.variables["APPWRITE_FUNCTION_PROJECT_ID"])
.setKey(req.variables["APPWRITE_FUNCTION_API_KEY"]);

try {
while (true) {
const listFiles = await storage.listFiles(bucketId);
if (listFiles.total === 0) {
return res.json({
success: true,
});
}
const promises = listFiles.files.map((file) => storage.deleteFile(bucketId, file.$id));
await Promise.all(promises);
}
} catch (_err) {
return res.json({
success: false,
message: "Bucket not found.",
});
}
}

0 comments on commit 579db4f

Please sign in to comment.