# 5.2 Transferring Data using Bayun's SecureTransferUtility

#### Now in order to use Bayun’s S3Wrapper instead of the standard AWS S3 SDK classes, these code snippets above change to using “Secure” versions of the corresponding classes, as below:

* As you can see from the code snippets below, in general it should be possible to simply query-replace the following type-names appropriately to their secure versions, and in most situations that should be sufficient.
  * AmazonS3Client   --> SecureAmazonS3Client  &#x20;
  * TransferUtility  --> SecureTransferUtility
* Add the following to the import statements:

```java
import com.bayun.S3wrapper.SecureAmazonS3Client;
import com.bayun.S3wrapper.SecureTransferUtility;
```

### 5.2.1 Initialize the SecureTransferUtility

{% tabs %}
{% tab title="Java" %}

```java
SecureAmazonS3Client s3 = new SecureAmazonS3Client(credentialsProvider, appContext);
SecureTransferUtility transferUtility = new SecureTransferUtility(s3, appContext);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
 val s3 = SecureAmazonS3Client(credentialsProvider, appContext)
 val transferUtility = SecureTransferUtility(s3, appContext)
```

{% endtab %}
{% endtabs %}

### 5.2.2 Upload a File to Amazon S3 using SecureTransferUtility

{% tabs %}
{% tab title="Java" %}

```java
TransferObserver transferObserver = secureTransferUtility.secureUpload(
  MY_BUCKET,     /* The bucket to upload to */
  OBJECT_KEY,    /* The key for the uploaded object */
  MY_FILE,       /* The file where the data to upload exists */
  null           /* The transferListener to track progress */
);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
 val transferObserver: TransferObserver = secureTransferUtility.secureUpload(
            MY_BUCKET,  /* The bucket to upload to */
            OBJECT_KEY,  /* The key for the uploaded object */
            MY_FILE,  /* The file where the data to upload exists */
            null /* The transferListener to track progress */
        )
```

{% endtab %}
{% endtabs %}

### 5.2.3 Download a File from Amazon S3 using SecureTransferUtility

{% tabs %}
{% tab title="Java" %}

```java
TransferObserver transferObserver = secureTransferUtility.secureDownload(
  MY_BUCKET,     /* The bucket to download from */
  OBJECT_KEY,    /* The key for the object to download */
  MY_FILE,       /* The file to download the object to */
  null           /* The transferListener to track progress */
);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val transferObserver: TransferObserver = secureTransferUtility.secureDownload(
            MY_BUCKET,  /* The bucket to download from */
            OBJECT_KEY,  /* The key for the object to download */
            MY_FILE,  /* The file to download the object to */
            null /* The transferListener to track progress */
        )
```

{% endtab %}
{% endtabs %}

### 5.2.4 Tracking S3 Transfer Progress using TransferObserver

{% tabs %}
{% tab title="Java" %}

```java
TransferObserver transferObserver = secureTransferUtility.secureDownload(
  MY_BUCKET,     /* The bucket to download from */
  OBJECT_KEY,    /* The key for the object to download */
  MY_FILE,       /* The file to download the object to */
  null           /* The transferListener to track progress */
);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
 val transferObserver: TransferObserver = secureTransferUtility.secureDownload(
            MY_BUCKET,  /* The bucket to download from */
            OBJECT_KEY,  /* The key for the object to download */
            MY_FILE,  /* The file to download the object to */
            null /* The transferListener to track progress */
        )
```

{% endtab %}
{% endtabs %}

{% hint style="info" %}

#### By doing the above, all data written to S3 is automatically locked before upload and unlocked after download in such a manner that nobody other than the customer (and especially the developer) has access to any of the encryption keys or the data itself.

{% endhint %}
