AWS Java SDK ile Stook Nasıl Kullanılır?

Stook’u Java ile kullanabilmek için aşağıdaki açıklamaları takip ediniz:

AWS Java SDK’yı indirmek için aşağıdaki linke tıklayınız.

Bu SDK, kolay bir şekilde başlamanıza yardımcı olacak birçok kod örneğine sahiptir.

Aşağıda bir bucket oluşturmak ve obje yüklemek için örnek kod verilmiştir:

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 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");
   }

}

Hesabınızda ya da uygulamanızda bir problemle karşılaşırsanız support@medianova.com‘a mail atabilir ya da +90 212 275 5456 nolu telefondan destek ekibimize ulaşabilirsiniz.

Medianova Team

Etiket: