> For the complete documentation index, see [llms.txt](https://docs.poja.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.poja.io/examples/hello-world-with-s3-file-storage.md).

# Hello World with S3 file Storage

***

### Goal <a href="#goal" id="goal"></a>

We want **to add persistence using an S3 Bucket** to an [**Hello World Application**](https://poja.gitbook.io/poja-docs/examples/hello-world-but-straight-to-the-cloud) already hosted on Poja that exposes the <mark style="background-color:$info;">`/hello?name=your-name`</mark> endpoint, for which it :

* Writes a Hello World message inside a *.txt* file
* Uploads the file to an S3 bucket
* Returns a **presigned uri** to download the file

{% hint style="info" %}
Deploy in one click

Before diving into the step-by-step guide, you can simply click the <mark style="color:$primary;">**Deploy to Poja**</mark> button. This will instantly deploy [<mark style="color:$primary;">**this template**</mark>](https://github.com/poja-app/poja-fs-template) to your Poja account

<a href="https://console.poja.io/applications/create/clone/?templateId=9e25599a-cf73-4558-8697-b22273a6171b" class="button primary">Deploy to Poja</a>
{% endhint %}

### How-to in 2 steps

#### **Step 1: Enable file storage for the preprod environment**

Select the **preprod environment**, click on the **Edit** button on the **Poja Configuration** section and then:

* Enable the **File Storage**
* Click on the **Save** button

Once it’s done, wait for the deployment to finish.

<br>

<figure><img src="/files/smdYd5yITYTEYiF6SuK8" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
/health/bucket

Once the **File Storage** option is activated, `/health/bucket` endpoint will be added to the application. It returns a presigned URI which gives you read-only access to tiny files created in the bucket for testing purpose.
{% endhint %}

<figure><img src="/files/EGlI2PrrhEv9gyv9naV5" alt=""><figcaption></figcaption></figure>

#### Step 2: Code your endpoint

{% hint style="info" %}
The BucketComponent class

To interact with the S3 Bucket associated to your application, Poja provides the **BucketComponent** class which has **4 public methods:**

* `upload()`
* `download()` to download a specific file by specifying its name
* `presign()` to get a **presigned uri** to access a specific file for a specified duration
* `getBucketName()`

<br>
{% endhint %}

{% code title="Java" %}

```java
package com.my.company.service;

import static java.io.File.createTempFile;

import com.my.company.file.bucket.BucketComponent;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.Duration;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

@Service
@AllArgsConstructor
public class HelloWorldService {
  private final BucketComponent bucketComponent;

  @SneakyThrows
  public String uploadHelloWorldMessage(String name) {
    var fileSuffix = ".txt";
    var filePrefix = "hello-world-" + name;
    var bucketKey = filePrefix + fileSuffix;
    var fileToUpload = createTempFile(filePrefix, fileSuffix);
    writeMessageIntoFile("Hello World " + name + " !", fileToUpload);
    bucketComponent.upload(fileToUpload, bucketKey);
    return bucketComponent.presign(bucketKey, Duration.ofMinutes(5)).toString();
  }

  private void writeMessageIntoFile(String message, File file) throws IOException {
    FileWriter writer = new FileWriter(file);
    writer.write(message);
    writer.close();
  }
}

```

{% endcode %}

{% code title="Java" %}

```java
package com.my.company.endpoint.rest.controller;

import com.my.company.service.HelloWorldService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@AllArgsConstructor
public class HelloWorldController {
  private final HelloWorldService service;

  @GetMapping("/hello")
  public String helloWorld(@RequestParam String name) {
    return service.uploadHelloWorldMessage(name);
  }
}

```

{% endcode %}

To trigger a deployment, just **commit and push** the code to the **preprod branch** of the repository. Wait. And voilà!

<figure><img src="/files/zRSm1fk8SKuqoym8pKxh" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/5X8LkTtEjuB9j9TxSzsW" alt=""><figcaption></figcaption></figure>

***
