Search in sources :

Example 26 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class CopyObject method copyBucketObject.

// snippet-start:[s3.java2.copy_object.main]
public static String copyBucketObject(S3Client s3, String fromBucket, String objectKey, String toBucket) {
    String encodedUrl = null;
    try {
        encodedUrl = URLEncoder.encode(fromBucket + "/" + objectKey, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        System.out.println("URL could not be encoded: " + e.getMessage());
    }
    CopyObjectRequest copyReq = CopyObjectRequest.builder().copySource(encodedUrl).destinationBucket(toBucket).destinationKey(objectKey).build();
    try {
        CopyObjectResponse copyRes = s3.copyObject(copyReq);
        return copyRes.copyObjectResult().toString();
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return "";
}
Also used : CopyObjectRequest(software.amazon.awssdk.services.s3.model.CopyObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) UnsupportedEncodingException(java.io.UnsupportedEncodingException) CopyObjectResponse(software.amazon.awssdk.services.s3.model.CopyObjectResponse)

Example 27 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class CreateBucket method createBucket.

// snippet-start:[s3.java2.create_bucket_waiters.main]
public static void createBucket(S3Client s3Client, String bucketName) {
    try {
        S3Waiter s3Waiter = s3Client.waiter();
        CreateBucketRequest bucketRequest = CreateBucketRequest.builder().bucket(bucketName).build();
        s3Client.createBucket(bucketRequest);
        HeadBucketRequest bucketRequestWait = HeadBucketRequest.builder().bucket(bucketName).build();
        // Wait until the bucket is created and print out the response.
        WaiterResponse<HeadBucketResponse> waiterResponse = s3Waiter.waitUntilBucketExists(bucketRequestWait);
        waiterResponse.matched().response().ifPresent(System.out::println);
        System.out.println(bucketName + " is ready");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter)

Example 28 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class DeleteBucketPolicy method deleteS3BucketPolicy.

// snippet-start:[s3.java2.delete_bucket_policy.main]
// Delete the bucket policy
public static void deleteS3BucketPolicy(S3Client s3, String bucketName) {
    DeleteBucketPolicyRequest delReq = DeleteBucketPolicyRequest.builder().bucket(bucketName).build();
    try {
        s3.deleteBucketPolicy(delReq);
        System.out.println("Done!");
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
}
Also used : DeleteBucketPolicyRequest(software.amazon.awssdk.services.s3.model.DeleteBucketPolicyRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception)

Example 29 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class ExportEndpoints method downloadFromS3.

// Downloads files from an Amazon S3 bucket and writes them to the path location
public static void downloadFromS3(S3Client s3Client, String path, String s3BucketName, List<String> objectKeys) {
    try {
        for (String key : objectKeys) {
            GetObjectRequest objectRequest = GetObjectRequest.builder().bucket(s3BucketName).key(key).build();
            ResponseBytes<GetObjectResponse> objectBytes = s3Client.getObjectAsBytes(objectRequest);
            byte[] data = objectBytes.asByteArray();
            // Write the data to a local file
            String fileSuffix = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
            path = path + fileSuffix + ".gz";
            File myFile = new File(path);
            OutputStream os = new FileOutputStream(myFile);
            os.write(data);
        }
        System.out.println("Download finished.");
    } catch (S3Exception | NullPointerException | IOException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Date(java.util.Date) FileOutputStream(java.io.FileOutputStream) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File)

Example 30 with S3Exception

use of software.amazon.awssdk.services.s3.model.S3Exception in project aws-doc-sdk-examples by awsdocs.

the class PPEBoundingBoxFrame method getObjectBytes.

public static byte[] getObjectBytes(S3Client s3, String bucketName, String keyName) {
    try {
        GetObjectRequest objectRequest = GetObjectRequest.builder().key(keyName).bucket(bucketName).build();
        ResponseBytes<GetObjectResponse> objectBytes = s3.getObjectAsBytes(objectRequest);
        byte[] data = objectBytes.asByteArray();
        return data;
    } catch (S3Exception e) {
        System.err.println(e.awsErrorDetails().errorMessage());
        System.exit(1);
    }
    return null;
}
Also used : GetObjectResponse(software.amazon.awssdk.services.s3.model.GetObjectResponse) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest)

Aggregations

S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)56 ArrayList (java.util.ArrayList)13 GetObjectRequest (software.amazon.awssdk.services.s3.model.GetObjectRequest)12 GetObjectResponse (software.amazon.awssdk.services.s3.model.GetObjectResponse)11 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)9 IOException (java.io.IOException)7 S3Client (software.amazon.awssdk.services.s3.S3Client)7 S3Object (software.amazon.awssdk.services.s3.model.S3Object)6 S3Waiter (software.amazon.awssdk.services.s3.waiters.S3Waiter)5 OutputStream (java.io.OutputStream)4 HttpURLConnection (java.net.HttpURLConnection)4 URL (java.net.URL)4 ListObjectsRequest (software.amazon.awssdk.services.s3.model.ListObjectsRequest)4 ListObjectsResponse (software.amazon.awssdk.services.s3.model.ListObjectsResponse)4 Tag (software.amazon.awssdk.services.s3.model.Tag)4 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 HashMap (java.util.HashMap)3 CreateBucketRequest (software.amazon.awssdk.services.s3.model.CreateBucketRequest)3 HeadBucketRequest (software.amazon.awssdk.services.s3.model.HeadBucketRequest)3