Search in sources :

Example 1 with DeleteObjectRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectRequest in project stocator by CODAIT.

the class COSAPIClient method delete.

@Override
public boolean delete(String hostName, Path path, boolean recursive) throws IOException {
    String key = pathToKey(path);
    LOG.debug("Object name to delete {}. Path {}", key, path.toString());
    try {
        mClient.deleteObject(new DeleteObjectRequest(mBucket, key));
        memoryCache.removeFileStatus(path.toString());
        return true;
    } catch (AmazonServiceException e) {
        if (e.getStatusCode() != 404) {
            throw new IOException(e);
        }
    }
    LOG.warn("Delete on {} not found. Nothing to delete");
    return false;
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonServiceException(com.amazonaws.AmazonServiceException) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 2 with DeleteObjectRequest

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

the class S3ObjectOperations method main.

public static void main(String[] args) throws IOException {
    final String USAGE = "\n" + "Usage:\n" + "    <bucketName> <key>\n\n" + "Where:\n" + "    bucketName - the Amazon S3 bucket to create.\n\n" + "    key - the key to use.\n\n";
    if (args.length != 2) {
        System.out.println(USAGE);
        System.exit(1);
    }
    String bucketName = args[0];
    String key = args[1];
    // snippet-start:[s3.java2.s3_object_operations.upload]
    Region region = Region.US_WEST_2;
    s3 = S3Client.builder().region(region).build();
    createBucket(s3, bucketName, region);
    PutObjectRequest objectRequest = PutObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.putObject(objectRequest, RequestBody.fromByteBuffer(getRandomByteBuffer(10_000)));
    // snippet-end:[s3.java2.s3_object_operations.upload]
    // Multipart upload example
    String multipartKey = "multiPartKey";
    multipartUpload(bucketName, multipartKey);
    // snippet-start:[s3.java2.s3_object_operations.pagination]
    ListObjectsV2Request listObjectsReqManual = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    boolean done = false;
    while (!done) {
        ListObjectsV2Response listObjResponse = s3.listObjectsV2(listObjectsReqManual);
        for (S3Object content : listObjResponse.contents()) {
            System.out.println(content.key());
        }
        if (listObjResponse.nextContinuationToken() == null) {
            done = true;
        }
        listObjectsReqManual = listObjectsReqManual.toBuilder().continuationToken(listObjResponse.nextContinuationToken()).build();
    }
    // snippet-end:[s3.java2.s3_object_operations.pagination]
    // snippet-start:[s3.java2.s3_object_operations.iterative]
    ListObjectsV2Request listReq = ListObjectsV2Request.builder().bucket(bucketName).maxKeys(1).build();
    ListObjectsV2Iterable listRes = s3.listObjectsV2Paginator(listReq);
    // Process response pages
    listRes.stream().flatMap(r -> r.contents().stream()).forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-end:[s3.java2.s3_object_operations.iterative]
    // snippet-start:[s3.java2.s3_object_operations.stream]
    // Helper method to work with paginated collection of items directly
    listRes.contents().stream().forEach(content -> System.out.println(" Key: " + content.key() + " size = " + content.size()));
    // snippet-start:[s3.java2.s3_object_operations.forloop]
    for (S3Object content : listRes.contents()) {
        System.out.println(" Key: " + content.key() + " size = " + content.size());
    }
    // snippet-end:[s3.java2.s3_object_operations.forloop]
    // snippet-start:[s3.java2.s3_object_operations.download]
    GetObjectRequest getObjectRequest = GetObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.getObject(getObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.download]
    // snippet-start:[s3.java2.s3_object_operations.delete]
    DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(key).build();
    s3.deleteObject(deleteObjectRequest);
    // snippet-end:[s3.java2.s3_object_operations.delete]
    // Delete an object
    deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucketName).key(multipartKey).build();
    s3.deleteObject(deleteObjectRequest);
    deleteBucket(s3, bucketName);
    System.out.println("Done");
}
Also used : CompletedMultipartUpload(software.amazon.awssdk.services.s3.model.CompletedMultipartUpload) S3Object(software.amazon.awssdk.services.s3.model.S3Object) CreateMultipartUploadResponse(software.amazon.awssdk.services.s3.model.CreateMultipartUploadResponse) Random(java.util.Random) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response) ByteBuffer(java.nio.ByteBuffer) HeadBucketResponse(software.amazon.awssdk.services.s3.model.HeadBucketResponse) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) CreateBucketRequest(software.amazon.awssdk.services.s3.model.CreateBucketRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) WaiterResponse(software.amazon.awssdk.core.waiters.WaiterResponse) Region(software.amazon.awssdk.regions.Region) UploadPartRequest(software.amazon.awssdk.services.s3.model.UploadPartRequest) S3Client(software.amazon.awssdk.services.s3.S3Client) DeleteBucketRequest(software.amazon.awssdk.services.s3.model.DeleteBucketRequest) HeadBucketRequest(software.amazon.awssdk.services.s3.model.HeadBucketRequest) IOException(java.io.IOException) S3Waiter(software.amazon.awssdk.services.s3.waiters.S3Waiter) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) CompletedPart(software.amazon.awssdk.services.s3.model.CompletedPart) CompleteMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CompleteMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) CreateBucketConfiguration(software.amazon.awssdk.services.s3.model.CreateBucketConfiguration) RequestBody(software.amazon.awssdk.core.sync.RequestBody) CreateMultipartUploadRequest(software.amazon.awssdk.services.s3.model.CreateMultipartUploadRequest) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) ListObjectsV2Request(software.amazon.awssdk.services.s3.model.ListObjectsV2Request) ListObjectsV2Iterable(software.amazon.awssdk.services.s3.paginators.ListObjectsV2Iterable) Region(software.amazon.awssdk.regions.Region) S3Object(software.amazon.awssdk.services.s3.model.S3Object) GetObjectRequest(software.amazon.awssdk.services.s3.model.GetObjectRequest) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest) ListObjectsV2Response(software.amazon.awssdk.services.s3.model.ListObjectsV2Response)

Example 3 with DeleteObjectRequest

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

the class DeleteObjectNonVersionedBucket method main.

public static void main(String[] args) throws IOException {
    Regions clientRegion = Regions.DEFAULT_REGION;
    String bucketName = "*** Bucket name ***";
    String keyName = "*** Key name ****";
    try {
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard().withCredentials(new ProfileCredentialsProvider()).withRegion(clientRegion).build();
        s3Client.deleteObject(new DeleteObjectRequest(bucketName, keyName));
    } catch (AmazonServiceException e) {
        // The call was transmitted successfully, but Amazon S3 couldn't process
        // it, so it returned an error response.
        e.printStackTrace();
    } catch (SdkClientException e) {
        // Amazon S3 couldn't be contacted for a response, or the client
        // couldn't parse the response from Amazon S3.
        e.printStackTrace();
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonS3(com.amazonaws.services.s3.AmazonS3) SdkClientException(com.amazonaws.SdkClientException) AmazonServiceException(com.amazonaws.AmazonServiceException) ProfileCredentialsProvider(com.amazonaws.auth.profile.ProfileCredentialsProvider) Regions(com.amazonaws.regions.Regions)

Example 4 with DeleteObjectRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectRequest in project pravega by pravega.

the class S3ChunkStorage method doDelete.

@Override
protected void doDelete(ChunkHandle handle) throws ChunkStorageException {
    try {
        // check whether the chunk exists
        if (!checkExists(handle.getChunkName())) {
            throw new ChunkNotFoundException(handle.getChunkName(), "doDelete");
        }
        DeleteObjectRequest deleteRequest = DeleteObjectRequest.builder().bucket(this.config.getBucket()).key(getObjectPath(handle.getChunkName())).build();
        client.deleteObject(deleteRequest);
    } catch (Exception e) {
        throw convertException(handle.getChunkName(), "doDelete", e);
    }
}
Also used : ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) DeleteObjectRequest(software.amazon.awssdk.services.s3.model.DeleteObjectRequest) S3Exception(software.amazon.awssdk.services.s3.model.S3Exception) ChunkStorageException(io.pravega.segmentstore.storage.chunklayer.ChunkStorageException) ChunkNotFoundException(io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException) ChunkAlreadyExistsException(io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)

Example 5 with DeleteObjectRequest

use of software.amazon.awssdk.services.s3.model.DeleteObjectRequest in project iaf by ibissource.

the class AmazonS3FileSystem method deleteFile.

@Override
public void deleteFile(S3Object f) throws FileSystemException {
    try {
        DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, f.getKey());
        s3Client.deleteObject(deleteObjectRequest);
    } catch (AmazonServiceException e) {
        throw new FileSystemException(e);
    }
}
Also used : DeleteObjectRequest(com.amazonaws.services.s3.model.DeleteObjectRequest) AmazonServiceException(com.amazonaws.AmazonServiceException)

Aggregations

DeleteObjectRequest (com.amazonaws.services.s3.model.DeleteObjectRequest)10 AmazonServiceException (com.amazonaws.AmazonServiceException)5 IOException (java.io.IOException)4 AmazonS3 (com.amazonaws.services.s3.AmazonS3)3 DeleteVersionRequest (com.amazonaws.services.s3.model.DeleteVersionRequest)3 AmazonClientException (com.amazonaws.AmazonClientException)2 SdkClientException (com.amazonaws.SdkClientException)2 Regions (com.amazonaws.regions.Regions)2 InterruptedIOException (java.io.InterruptedIOException)2 HashMap (java.util.HashMap)2 Test (org.junit.Test)2 DeleteObjectRequest (software.amazon.awssdk.services.s3.model.DeleteObjectRequest)2 S3Exception (software.amazon.awssdk.services.s3.model.S3Exception)2 ProfileCredentialsProvider (com.amazonaws.auth.profile.ProfileCredentialsProvider)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 ChunkAlreadyExistsException (io.pravega.segmentstore.storage.chunklayer.ChunkAlreadyExistsException)1 ChunkNotFoundException (io.pravega.segmentstore.storage.chunklayer.ChunkNotFoundException)1 ChunkStorageException (io.pravega.segmentstore.storage.chunklayer.ChunkStorageException)1 ByteBuffer (java.nio.ByteBuffer)1 Random (java.util.Random)1