-
Notifications
You must be signed in to change notification settings - Fork 125
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Aragur/main
⚡ Add a Deno example to wipe an Appwrite bucket
- Loading branch information
Showing
3 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.", | ||
}); | ||
} | ||
} |