Skip to main content

How do I use Stook with the AWS Java SDK?

To use Stook with Java, follow the steps below:

  • Click the link for AWS Java SDK.

  • SDK has many code examples to help you begin easily.

  • This sample code is given below to create a bucket and upload an object.

CODE
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;
CODE
public class s3Exapmle {
    
   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)) {
           // Because the CreateBucketRequest object doesn't specify a region, the
           // bucket is created in the region specified in the client.
           AMAZON_S3_CLIENT.createBucket(new CreateBucketRequest(BUCKET_NAME));

           // Verify that the bucket was created by retrieving it and checking its location.
           String bucketLocation = AMAZON_S3_CLIENT.getBucketLocation(new GetBucketLocationRequest(BUCKET_NAME));
           System.out.println("Bucket location: " + bucketLocation);
       }

           try {
                uploadFile("Hello World".getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
       System.out.println("Hello, World");
   }

}

JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.