|
| 1 | +--- |
| 2 | +id: samples-s3-keploy |
| 3 | +title: Sample S3 File Manager App (Golang) |
| 4 | +sidebar_label: Fiber + S3 |
| 5 | +description: The following sample app showcases how to use fiber framework with AWS S3 and the Keploy Platform. |
| 6 | +tags: |
| 7 | + - go |
| 8 | + - quickstart |
| 9 | + - samples |
| 10 | + - examples |
| 11 | + - tutorial |
| 12 | + - s3 |
| 13 | + - fiber-framework |
| 14 | +keyword: |
| 15 | + - Fiber Framework |
| 16 | + - AWS S3 Mock |
| 17 | + - Golang |
| 18 | + - API Test generator |
| 19 | + - Auto Testcase generation |
| 20 | +--- |
| 21 | + |
| 22 | +## Introduction |
| 23 | + |
| 24 | +πͺ Dive into the world of cloud storage management and see how seamlessly Keploy integrates with Fiber and AWS S3! Buckle up, it's gonna be a fun ride! π’ |
| 25 | + |
| 26 | +import InstallationGuide from '../concepts/installation.md' |
| 27 | + |
| 28 | +<InstallationGuide/> |
| 29 | + |
| 30 | +## Get Started! π¬ |
| 31 | + |
| 32 | +## Clone the sample S3 file manager app π§ͺ |
| 33 | + |
| 34 | +```bash |
| 35 | +git clone https://github.com/keploy/samples-go.git && cd samples-go/S3-Keploy |
| 36 | +go mod download |
| 37 | +``` |
| 38 | + |
| 39 | +## Prerequisites π§ |
| 40 | + |
| 41 | +Before we start, make sure you have: |
| 42 | + |
| 43 | +1. [Go](https://go.dev/doc/install) installed |
| 44 | +2. [AWS Access Key and Secret Key](https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/welcome.html) |
| 45 | +3. AWS credentials configured |
| 46 | + |
| 47 | +### Setting AWS Credentials π |
| 48 | + |
| 49 | +Go to your home directory and create `.aws` folder: |
| 50 | + |
| 51 | +```bash |
| 52 | +mkdir ~/.aws |
| 53 | +``` |
| 54 | + |
| 55 | +Create a `credentials` file inside `.aws` folder: |
| 56 | + |
| 57 | +```bash |
| 58 | +touch ~/.aws/credentials |
| 59 | +``` |
| 60 | + |
| 61 | +Open `credentials` in any text editor and add the following: |
| 62 | + |
| 63 | +``` |
| 64 | +[default] |
| 65 | +aws_access_key_id = <YOUR_ACCESS_KEY_ID> |
| 66 | +aws_secret_access_key = <YOUR_SECRET_ACCESS_KEY> |
| 67 | +``` |
| 68 | + |
| 69 | +## Running the Application π |
| 70 | + |
| 71 | +We'll be running our sample application locally with AWS S3 integration. Ready? Let's get the party started! π |
| 72 | + |
| 73 | +If you are using WSL on Windows then use below to start wsl in the user's home directory: |
| 74 | + |
| 75 | +```bash |
| 76 | +wsl ~ |
| 77 | +``` |
| 78 | + |
| 79 | +### Recording Test Cases πΌ |
| 80 | + |
| 81 | +Ready, set, record! Here's how: |
| 82 | + |
| 83 | +```bash |
| 84 | +sudo -E env PATH="$PATH" keploy record -c 'go run .' |
| 85 | +``` |
| 86 | + |
| 87 | +Keep an eye out for the `-c` flag! It's the command charm to run the app. Whether you're using `go run .` or `go run main.go`, it's your call. |
| 88 | + |
| 89 | +If you're seeing logs that resemble the ones below, you're on the right track: |
| 90 | + |
| 91 | + <img src="/docs/img/code-snippets/keploy-record-fiber-s3-local.png" alt="Keploy Record Test case locally" width="100%" /> |
| 92 | + |
| 93 | +π₯ Challenge time! Generate some test cases. How? Just **make some API calls**. Postman, Hoppscotch or even curl - take your pick! |
| 94 | + |
| 95 | +Let's manage some S3 buckets and files: |
| 96 | + |
| 97 | +#### Create a new bucket |
| 98 | + |
| 99 | +```bash |
| 100 | +curl --request POST \ |
| 101 | + --url http://localhost:3000/create \ |
| 102 | + --header 'content-type: application/json' \ |
| 103 | + --data '{ |
| 104 | + "name": "my-test-bucket-keploy-2024" |
| 105 | + }' |
| 106 | +``` |
| 107 | + |
| 108 | +Here's a peek of what you get: |
| 109 | + |
| 110 | +```json |
| 111 | +{ |
| 112 | + "msg": "my-test-bucket-keploy-2024 Bucket created successfully!" |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### List all buckets |
| 117 | + |
| 118 | +```bash |
| 119 | +curl --request GET \ |
| 120 | + --url http://localhost:3000/list |
| 121 | +``` |
| 122 | + |
| 123 | +Response: |
| 124 | + |
| 125 | +```json |
| 126 | +{ |
| 127 | + "buckets": ["my-test-bucket-keploy-2024", "another-bucket-name"] |
| 128 | +} |
| 129 | +``` |
| 130 | + |
| 131 | +π Woohoo! With simple API calls, you've crafted test cases with mocks! Dive into the Keploy directory and feast your eyes on the newly minted `test-1.yml` and `mocks.yml` |
| 132 | + |
| 133 | +```yaml |
| 134 | +version: api.keploy.io/v1beta2 |
| 135 | +kind: Http |
| 136 | +name: test-1 |
| 137 | +spec: |
| 138 | + metadata: {} |
| 139 | + req: |
| 140 | + method: POST |
| 141 | + proto_major: 1 |
| 142 | + proto_minor: 1 |
| 143 | + url: http://localhost:3000/create |
| 144 | + header: |
| 145 | + Accept: "*/*" |
| 146 | + Content-Length: "35" |
| 147 | + Content-Type: application/json |
| 148 | + Host: localhost:3000 |
| 149 | + User-Agent: curl/7.77.0 |
| 150 | + body: |- |
| 151 | + { |
| 152 | + "name": "my-test-bucket-keploy-2024" |
| 153 | + } |
| 154 | + body_type: "" |
| 155 | + resp: |
| 156 | + status_code: 200 |
| 157 | + header: |
| 158 | + Content-Length: "55" |
| 159 | + Content-Type: application/json; charset=utf-8 |
| 160 | + Date: Wed, 18 Jun 2025 10:15:47 GMT |
| 161 | + body: '{"msg":"my-test-bucket-keploy-2024 Bucket created successfully!"}' |
| 162 | + body_type: "" |
| 163 | + status_message: "" |
| 164 | + proto_major: 0 |
| 165 | + proto_minor: 0 |
| 166 | + objects: [] |
| 167 | + assertions: |
| 168 | + noise: |
| 169 | + - header.Date |
| 170 | + created: 1718705747 |
| 171 | +``` |
| 172 | +
|
| 173 | +This is how the generated **mock.yml** will look like: |
| 174 | +
|
| 175 | +```yaml |
| 176 | +version: api.keploy.io/v1beta2 |
| 177 | +kind: Http |
| 178 | +name: mocks |
| 179 | +spec: |
| 180 | + metadata: |
| 181 | + operation: "CreateBucket" |
| 182 | + requests: |
| 183 | + - header: |
| 184 | + Authorization: "AWS4-HMAC-SHA256 Credential=..." |
| 185 | + Content-Type: "application/x-amz-json-1.0" |
| 186 | + X-Amz-Target: "DynamoDB_20120810.CreateBucket" |
| 187 | + body: '{"BucketName":"my-test-bucket-keploy-2024","Region":"ap-south-1"}' |
| 188 | + method: POST |
| 189 | + url: "https://s3.ap-south-1.amazonaws.com/" |
| 190 | + responses: |
| 191 | + - status_code: 200 |
| 192 | + header: |
| 193 | + Content-Type: "application/x-amz-json-1.0" |
| 194 | + Date: "Wed, 18 Jun 2025 10:15:47 GMT" |
| 195 | + body: '{"BucketLocation":"ap-south-1"}' |
| 196 | + created: 1718705747 |
| 197 | +``` |
| 198 | +
|
| 199 | +_Time to perform more API magic!_ |
| 200 | +
|
| 201 | +#### Upload a file to bucket |
| 202 | +
|
| 203 | +```bash |
| 204 | +curl --request POST \ |
| 205 | + --url "http://localhost:3000/upload?bucket=my-test-bucket-keploy-2024" \ |
| 206 | + |
| 207 | +``` |
| 208 | + |
| 209 | +#### Get all objects in a bucket |
| 210 | + |
| 211 | +```bash |
| 212 | +curl --request GET \ |
| 213 | + --url "http://localhost:3000/getallobjects?bucket=my-test-bucket-keploy-2024" |
| 214 | +``` |
| 215 | + |
| 216 | +#### Replace/Update a file |
| 217 | + |
| 218 | +```bash |
| 219 | +curl --request PUT \ |
| 220 | + --url "http://localhost:3000/replacefile?bucket=my-test-bucket-keploy-2024" \ |
| 221 | + |
| 222 | +``` |
| 223 | + |
| 224 | +#### Delete all objects in a bucket |
| 225 | + |
| 226 | +```bash |
| 227 | +curl --request DELETE \ |
| 228 | + --url "http://localhost:3000/deleteallobjects?bucket=my-test-bucket-keploy-2024" |
| 229 | +``` |
| 230 | + |
| 231 | +#### Delete a bucket |
| 232 | + |
| 233 | +```bash |
| 234 | +curl --request DELETE \ |
| 235 | + --url "http://localhost:3000/delete?bucket=my-test-bucket-keploy-2024" |
| 236 | +``` |
| 237 | + |
| 238 | +Spotted the new test and mock files in your project? High five! π |
| 239 | + |
| 240 | +<img src="/docs/img/code-snippets/fiber-s3-test-sample-local.png" alt="Sample Keploy Test case and Mock for Fiber S3" width="100%" style={{ borderRadius: '5px' }} /> |
| 241 | + |
| 242 | +### Running Tests πββοΈ |
| 243 | + |
| 244 | +Time to put things to the test π§ͺ |
| 245 | + |
| 246 | +```bash |
| 247 | +sudo -E env PATH=$PATH keploy test -c "go run ." --delay 20 |
| 248 | +``` |
| 249 | + |
| 250 | +> The `--delay` flag? Oh, that's just giving your app a little breather (in seconds) before the test cases come knocking. This is especially important for S3 operations as they might need some time to propagate across AWS infrastructure. |
| 251 | +
|
| 252 | +Your results should be looking all _snazzy_, like this: |
| 253 | + |
| 254 | +<img src="/docs/img/code-snippets/s3-fiber-test-result-local.png" alt="Sample Keploy Test Result Fiber S3" width="100%" style={{ borderRadius: '5px' }}/> |
| 255 | + |
| 256 | +Did you spot any AWS-specific fields showing differences? That's totally normal with cloud services! βοΈ |
| 257 | + |
| 258 | +Worry not, just add the ever-changing fields (like AWS **signatures**, **timestamps**, or **request IDs**) to the **noise parameter** to **dodge those assertions**. |
| 259 | + |
| 260 | +> Pro tip: Add `header.Authorization`, `header.X-Amz-Date`, `header.X-Amz-Request-Id` to noise in `test-x.yaml` if needed. |
| 261 | +
|
| 262 | +<img src="/docs/img/code-snippets/noise-addition-s3-local.png" alt="Adding Noise to Test case Fiber S3" width="70%" style={{ borderRadius: '5px' }}/> |
| 263 | + |
| 264 | +Run that `keploy test` command once more and watch as everything falls into place with all tests passing! π |
| 265 | + |
| 266 | +Final thoughts? Dive deeper! Try different API calls, tweak the S3 response in the `mocks.yml`, or fiddle with the request or response in `test-x.yml`. Run the tests again and see the magic unfold! β¨π©βπ»π¨βπ»β¨ |
| 267 | + |
| 268 | +## Available API Endpoints π£οΈ |
| 269 | + |
| 270 | +Here's a quick reference of all available endpoints: |
| 271 | + |
| 272 | +- `GET /list` - Get all bucket names |
| 273 | +- `GET /getallobjects?bucket=<BUCKET_NAME>` - Get all objects in a specific bucket |
| 274 | +- `POST /create` - Create a new bucket |
| 275 | +- `POST /upload?bucket=<BUCKET_NAME>` - Upload a file to a bucket |
| 276 | +- `PUT /replacefile?bucket=<BUCKET_NAME>` - Replace/update an existing file |
| 277 | +- `DELETE /delete?bucket=<BUCKET_NAME>` - Delete a bucket |
| 278 | +- `DELETE /deleteallobjects?bucket=<BUCKET_NAME>` - Delete all objects in a bucket |
| 279 | + |
| 280 | +## Troubleshooting π§ |
| 281 | + |
| 282 | +**AWS Credentials Issues:** |
| 283 | + |
| 284 | +- Make sure your AWS credentials are properly configured in `~/.aws/credentials` |
| 285 | +- Ensure your IAM user has appropriate S3 permissions |
| 286 | +- Check that the region is set correctly (default: `ap-south-1`) |
| 287 | + |
| 288 | +**Bucket Name Issues:** |
| 289 | + |
| 290 | +- S3 bucket names must be globally unique |
| 291 | +- Use lowercase letters, numbers, and hyphens only |
| 292 | +- Avoid dots in bucket names |
| 293 | + |
| 294 | +**File Upload Issues:** |
| 295 | + |
| 296 | +- Make sure the file exists in your current directory |
| 297 | +- Check file permissions |
| 298 | +- Ensure the bucket exists before uploading |
| 299 | + |
| 300 | +## Wrapping it up π |
| 301 | + |
| 302 | +Congrats on the journey so far! You've seen Keploy's power with Fiber and AWS S3, flexed your coding muscles, and had a bit of fun too! Now, go out there and keep exploring, innovating, and creating! Remember, with the right tools and a sprinkle of fun, anything's possible. ππ |
| 303 | + |
| 304 | +Happy coding! β¨π©βπ»π¨βπ»β¨ |
| 305 | + |
| 306 | +Hope this helps you out, if you still have any questions, reach out to us. |
| 307 | + |
| 308 | +import GetSupport from '../concepts/support.md' |
| 309 | + |
| 310 | +<GetSupport/> |
0 commit comments