# 5.1 Transferring Data using standard TransferUtility

Add the following to the import statements:

```java
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferObserver;
import com.amazonaws.mobileconnectors.s3.transferutility.TransferUtility;
```

Create a credentials provider so you can easily integrate AWS S3 with your Android application. You pass the credentials provider object to the constructor of the AWS client you are using. The credentials provider looks like this:

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

```java
AWSCredentials credentialsProvider = new AWSCredentials() {
    @Override
    public String getAWSAccessKeyId() {
        return AWS_ACCESS_KEY;
    }

    @Override
    public String getAWSSecretKey() {
        return AWS_SECRET_KEY;
    }
};                       
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
 val credentialsProvider: AWSCredentials = object : AWSCredentials() {
            val aWSAccessKeyId: String?
                get() = AWS_ACCESS_KEY
            val aWSSecretKey: String?
                get() = AWS_SECRET_KEY
        }
```

{% endtab %}
{% endtabs %}

### 5.1.1 Initialize the S3 TransferUtility

First, pass your Amazon credentials provider to the AmazonS3Client constructor. Then, pass the client to the TransferUtility constructor along with the application context:

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

```java
AmazonS3Client s3 = new AmazonS3Client(credentialsProvider, appContext);
TransferUtility transferUtility = new TransferUtility(s3, appContext);// Some code
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val s3 = AmazonS3Client(credentialsProvider, appContext)
val transferUtility = TransferUtility(s3, appContext)
```

{% endtab %}
{% endtabs %}

### 5.1.2 Upload a File to Amazon S3

To upload a file to S3, instantiate a TransferObserver object. Call upload() on your TransferUtility object and assign it to the observer, passing the following parameters:

* **bucket\_name** – Name of the S3 bucket to store the file
* **key** – Name of the file, once stored in S3  &#x20;
* **file** – java.io.File object to upload &#x20;

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

```java
TransferObserver transferObserver = transferUtility.upload(
  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 */
);                  
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val transferObserver: TransferObserver = transferUtility.upload(
            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 */
        )
```

{% endtab %}
{% endtabs %}

Uploads automatically use S3’s multi-part upload functionality on large files to enhance throughput.

### 5.1.3 Download a File from Amazon S3

To download a file from S3, instantiate a TransferObserver object. Call download() on your TransferUtility object and assign it to the observer, passing the following parameters:

* **bucket\_name** – A string representing the name of the S3 bucket where the file is stored &#x20;
* **key** – A string representing the name of the S3 object (a file in this case) to download &#x20;
* **file** – the java.io.File object where the downloaded file will be written

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

```java
TransferObserver transferObserver = transferUtility.download(
  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 */
);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
val transferObserver: TransferObserver = transferUtility.download(
            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 */
        )
```

{% endtab %}
{% endtabs %}

### 5.1.4 Tracking S3 Transfer Progress

With the TransferUtility, the download() and upload() methods return a TransferObserver object. This object gives access to:

* The state (now specified as an enum)
* The total bytes transferred thus far
* The total bytes to transfer (for easily calculating progress bars)
* A unique ID that you can use to keep track of distinct transfers

Given the transfer ID, this `TransferObserver` object can be retrieved from anywhere in your app, including if the app is killed. It also lets you create a `TransferListener`, which will be updated on state or progress change, as well as when an error occurs.

To get the progress of a download or upload, call `setTransferListener()` on your `TransferObserver`. This requires you to implement `onStateChanged`, `onProgressChanged`, and `onError`. For example:

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

```java
TransferListener listener = new TransferListener(){

    @Override
    public void onStateChanged(int id, TransferState state) {
        // do something
    }

    @Override
    public void onProgressChanged(int id, long bytesCurrent, long bytesTotal) {
        int percentage = (int) (bytesCurrent/bytesTotal * 100);
        //Display percentage transffered to user
    }

    @Override
    public void onError(int id, Exception ex) {
        // do something
    }
};

TransferObserver transferObserver = transferUtility.download(MY_BUCKET, OBJECT_KEY, MY_FILE, listener);
```

{% endtab %}

{% tab title="Kotlin" %}

```kotlin
 val listener: TransferListener = object : TransferListener() {
            fun onStateChanged(id: Int, state: TransferState?) {
                // do something
            }

            fun onProgressChanged(id: Int, bytesCurrent: Long, bytesTotal: Long) {
                val percentage = (bytesCurrent / bytesTotal * 100).toInt()
                //Display percentage transffered to user
            }

            fun onError(id: Int, ex: Exception?) {
                // do something
            }
        }

        val transferObserver: TransferObserver =
            transferUtility.download(MY_BUCKET, OBJECT_KEY, MY_FILE, listener)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://bayun.gitbook.io/bayun-awss3-wrapper-android-programming-guide/store_and_retrieve_files_securely_from_amazon_s3/5.1-transferring-data-using-standard-transferutility.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
