@Override public boolean doesBucketExist(String bucketName) throws SdkClientException, AmazonServiceException { try { headBucket(new HeadBucketRequest(bucketName)); return true; } catch (AmazonServiceException ase) { // A redirect error or a forbidden error means the bucket exists. So // returning true. if ((ase.getStatusCode() == Constants.BUCKET_REDIRECT_STATUS_CODE) || (ase.getStatusCode() == Constants.BUCKET_ACCESS_FORBIDDEN_STATUS_CODE)) { return true; } if (ase.getStatusCode() == Constants.NO_SUCH_BUCKET_STATUS_CODE) { return false; } throw ase; } }
/** * Retrieves the region of the bucket by making a HeadBucket request to us-west-1 region. * * Currently S3 doesn't return region in a HEAD Bucket request if the bucket * owner has enabled bucket to accept only SigV4 requests via bucket * policies. */ private String getBucketRegionViaHeadRequest(String bucketName) { String bucketRegion = null; try { Request<HeadBucketRequest> request = createRequest(bucketName, null, new HeadBucketRequest(bucketName), HttpMethodName.HEAD); HeadBucketResult result = invoke(request, new HeadBucketResultHandler(), bucketName, null, true); bucketRegion = result.getBucketRegion(); } catch (AmazonS3Exception exception) { if (exception.getAdditionalDetails() != null) { bucketRegion = exception.getAdditionalDetails().get( Headers.S3_BUCKET_REGION); } } if (bucketRegion == null && log.isDebugEnabled()) { log.debug("Not able to derive region of the " + bucketName + " from the HEAD Bucket requests."); } return bucketRegion; }
@Test public void removeBucket() throws Exception { String bucketName = "testremovebkbk" + UUID.randomUUID().toString(); // remove bucket not exist Thread.sleep(3000); RGW_ADMIN.removeBucket(bucketName); testWithAUser( v -> { String userId = "testremovebk" + UUID.randomUUID().toString(); User response = RGW_ADMIN.createUser(userId); AmazonS3 s3 = createS3( response.getS3Credentials().get(0).getAccessKey(), response.getS3Credentials().get(0).getSecretKey()); s3.createBucket(bucketName); ByteArrayInputStream input = new ByteArrayInputStream("Hello World!".getBytes()); s3.putObject(bucketName, "hello.txt", input, new ObjectMetadata()); RGW_ADMIN.removeBucket(bucketName); try { s3.headBucket(new HeadBucketRequest(bucketName)); fail(); } catch (Exception e) { assertTrue("Not Found".equals(((AmazonS3Exception) e).getErrorMessage())); } }); }
@Test public void unlinkBucket() throws Exception { testWithAUser( (v) -> { String userId = v.getUserId(); AmazonS3 s3 = createS3( v.getS3Credentials().get(0).getAccessKey(), v.getS3Credentials().get(0).getSecretKey()); String bucketName = userId.toLowerCase(); // not exist RGW_ADMIN.unlinkBucket(bucketName, userId); s3.createBucket(bucketName); // basic RGW_ADMIN.unlinkBucket(bucketName, userId); assertEquals(0, s3.listBuckets().size()); // head is ok... s3.headBucket(new HeadBucketRequest(bucketName)); // again RGW_ADMIN.unlinkBucket(bucketName, userId); }); }
@Override public HeadBucketResult headBucket(HeadBucketRequest headBucketRequest) throws SdkClientException, AmazonServiceException { headBucketRequest = beforeClientExecution(headBucketRequest); String bucketName = headBucketRequest.getBucketName(); rejectNull(bucketName, "The bucketName parameter must be specified."); Request<HeadBucketRequest> request = createRequest(bucketName, null, headBucketRequest, HttpMethodName.HEAD); return invoke(request, new HeadBucketResultHandler(), bucketName, null); }
@Test public void testContainerExists() throws Exception { client.headBucket(new HeadBucketRequest(containerName)); try { client.headBucket(new HeadBucketRequest( createRandomContainerName())); Fail.failBecauseExceptionWasNotThrown(AmazonS3Exception.class); } catch (AmazonS3Exception e) { assertThat(e.getErrorCode()).isEqualTo("404 Not Found"); } }
@Test public void testContainerDelete() throws Exception { client.headBucket(new HeadBucketRequest(containerName)); client.deleteBucket(containerName); try { client.headBucket(new HeadBucketRequest(containerName)); Fail.failBecauseExceptionWasNotThrown(AmazonS3Exception.class); } catch (AmazonS3Exception e) { assertThat(e.getErrorCode()).isEqualTo("404 Not Found"); } }
@SuppressWarnings("unchecked") public static void createBucket(AmazonS3Client client, String bucketName) throws Exception { client.createBucket(bucketName); HeadBucketRequest request = new HeadBucketRequest(bucketName); Waiter<HeadBucketRequest> waiter = client.waiters().bucketExists(); Future<Void> future = waiter.runAsync(new WaiterParameters<HeadBucketRequest>(request), new NoOpWaiterHandler()); future.get(1, TimeUnit.MINUTES); }
@Override public HeadBucketResult headBucket(HeadBucketRequest headBucketRequest) throws AmazonClientException, AmazonServiceException { return delegate.headBucket(headBucketRequest); }
@Override public HeadBucketResult headBucket(HeadBucketRequest headBucketRequest) throws SdkClientException, AmazonServiceException { return call(() -> getDelegate().headBucket(headBucketRequest)); }
public String FindOrCreateWeatherPipeJobDirectory() { String bucketLocation = null; try { if(!(s3client.doesBucketExist(jobBucketName))) { // Note that CreateBucketRequest does not specify region. So bucket is // created in the region specified in the client. s3client.createBucket(new CreateBucketRequest( jobBucketName)); } else { s3client.headBucket(new HeadBucketRequest(jobBucketName)); } bucketLocation = "s3n://" + jobBucketName + "/"; } catch (AmazonServiceException ase) { if(ase.getStatusCode() == 403) { System.out.println("You do not have propper permissions to access " + jobBucketName + ". S3 uses a global name space, please make sure you are using a unique bucket name."); System.exit(1); } else { System.out.println("Caught an AmazonServiceException, which " + "means your request made it " + "to Amazon S3, but was rejected with an error response" + " for some reason."); System.out.println("Error Message: " + ase.getMessage()); System.out.println("HTTP Status Code: " + ase.getStatusCode()); System.out.println("AWS Error Code: " + ase.getErrorCode()); System.out.println("Error Type: " + ase.getErrorType()); System.out.println("Request ID: " + ase.getRequestId()); } System.exit(1); } catch (AmazonClientException ace) { System.out.println("Caught an AmazonClientException, which " + "means the client encountered " + "an internal error while trying to " + "communicate with S3, " + "such as not being able to access the network."); System.out.println("Error Message: " + ace.getMessage()); System.exit(1); } return bucketLocation; }
/** * Makes a call to the operation specified by the waiter by taking the * corresponding request and returns the corresponding result * * @param headBucketRequest * Corresponding request for the operation * @return Corresponding result of the operation */ @Override public HeadBucketResult apply(HeadBucketRequest headBucketRequest) { return client.headBucket(headBucketRequest); }