# How do I use Stook with the AWS Java SDK?

You can connect Java applications to **Medianova Stook Object Storage** using the **AWS SDK for Java**. This allows you to create buckets, upload files, and manage objects directly from your Java code.

***

### Prerequisites

* JDK installed on your system
* AWS SDK for Java
* Medianova Stook credentials:
  * **Access Key**
  * **Secret Key**
  * **Endpoint**

***

### Installation

Download [the AWS SDK for Java from the official site](https://aws.amazon.com/sdk-for-java/):

***

### Example: Create a Bucket and Upload File

```java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.model.GetBucketLocationRequest;

public class s3Example {
    
   private static final String SERVICE_ENDPOINT = "https://******.mncdn.com";
   private static final String REGION = "us-east-1";
   private static final String ACCESS_KEY = "******";
   private static final String SECRET_KEY = "******";

   private static final String BUCKET_NAME = "bucket";

   private static final AmazonS3 AMAZON_S3_CLIENT = 
       AmazonS3ClientBuilder.standard()
       .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(SERVICE_ENDPOINT, REGION))
       .withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)))
       .withPathStyleAccessEnabled(true)
       .build();

   public static String uploadFile(byte[] data) throws IOException {
       try (InputStream inputStream = new ByteArrayInputStream(data)) {
           String filename = "test.txt";

           ObjectMetadata metadata = new ObjectMetadata();
           metadata.setContentLength(data.length);

           PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, filename, inputStream, metadata)
                   .withCannedAcl(CannedAccessControlList.PublicRead);

           AMAZON_S3_CLIENT.putObject(putObjectRequest);

           return AMAZON_S3_CLIENT.getUrl(BUCKET_NAME, filename).toString();
       }
   }

   public static void main(String[] args) {
       if (!AMAZON_S3_CLIENT.doesBucketExistV2(BUCKET_NAME)) {
           AMAZON_S3_CLIENT.createBucket(new CreateBucketRequest(BUCKET_NAME));

           String bucketLocation = AMAZON_S3_CLIENT.getBucketLocation(new GetBucketLocationRequest(BUCKET_NAME));
           System.out.println("Bucket location: " + bucketLocation);
       }

       try {
           uploadFile("Hello World".getBytes());
       } catch (IOException e) {
           e.printStackTrace();
       }
       System.out.println("Hello, World");
   }
}
```

*Example Java code for connecting to Stook, creating a bucket, and uploading a file*

***

### Troubleshooting / FAQ

* **Bucket already exists error:** Use a unique bucket name.
* **Connection error:** Verify endpoint URL and credentials.
* **Access denied:** Check Access Key and Secret Key.

***

### References

* [AWS SDK for Java Official Documentation](https://aws.amazon.com/sdk-for-java/)
