Java Code Examples for com.amazonaws.services.s3.AmazonS3.getObjectMetadata()

The following are Jave code examples for showing how to use getObjectMetadata() of the com.amazonaws.services.s3.AmazonS3 class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: opentest   File: GetS3Metadata.java   View Source Code Vote up 5 votes
@Override
public void run() {
    super.run();

    String awsCredentialsProfile = this.readStringArgument("awsProfile", "default");
    String bucket = this.readStringArgument("bucket");
    String objectKey = this.readStringArgument("objectKey");

    AmazonS3 s3Client = new AmazonS3Client(new ProfileCredentialsProvider(awsCredentialsProfile));
    ObjectMetadata metadata = s3Client.getObjectMetadata(
            new GetObjectMetadataRequest(bucket, objectKey));

    try {
        Date expirationTime = metadata.getExpirationTime();
        if (expirationTime != null) {
            this.writeOutput("expirationTime", metadata.getExpirationTime().getTime());
        } else {
            this.writeOutput("expirationTime", null);
        }
        this.writeOutput("lastModified", metadata.getLastModified().getTime());
        this.writeOutput("userMetadata", metadata.getUserMetadata());
        this.writeOutput("size", metadata.getContentLength());
        this.writeOutput("storageClass", metadata.getStorageClass());
        this.writeOutput("versionId", metadata.getVersionId());
    } catch (Exception ex) {
        throw new RuntimeException(String.format(
                "Failed to get object metadata for object key %s in bucket %s",
                objectKey,
                bucket), ex);
    }
}
 
Example 2
Project: S3Decorators   File: HystrixS3DecoratorTest.java   View Source Code Vote up 5 votes
@Test
public void itDoesntCount404AsFailure() throws InterruptedException {
  AmazonS3 s3 = HystrixS3Decorator.decorate(new MissingS3Client());

  for (int i = 0; i < 100; i++ ) {
    try {
      s3.getObjectMetadata("test-bucket", "test-key");
    } catch (AmazonServiceException e) {
      assertThat(e.getStatusCode()).isEqualTo(404);
    }

    Thread.sleep(50);
  }
}
 
Example 3
Project: S3Decorators   File: HystrixS3DecoratorTest.java   View Source Code Vote up 5 votes
@Test
public void itDoesntCount403AsFailure() throws InterruptedException {
  AmazonS3 s3 = HystrixS3Decorator.decorate(new NotAuthedS3Client());

  for (int i = 0; i < 100; i++ ) {
    try {
      s3.getObjectMetadata("test-bucket", "test-key");
    } catch (AmazonServiceException e) {
      assertThat(e.getStatusCode()).isEqualTo(403);
    }

    Thread.sleep(50);
  }
}
 
Example 4
Project: S3Decorators   File: FailsafeS3DecoratorTest.java   View Source Code Vote up 5 votes
@Test
public void itDoesntCount404AsFailure() throws InterruptedException {
  AmazonS3 s3 = FailsafeS3Decorator.decorate(new MissingS3Client());

  for (int i = 0; i < 100; i++ ) {
    try {
      s3.getObjectMetadata("test-bucket", "test-key");
    } catch (AmazonServiceException e) {
      assertThat(e.getStatusCode()).isEqualTo(404);
    }

    Thread.sleep(50);
  }
}
 
Example 5
Project: S3Decorators   File: FailsafeS3DecoratorTest.java   View Source Code Vote up 5 votes
@Test
public void itDoesntCount403AsFailure() throws InterruptedException {
  AmazonS3 s3 = FailsafeS3Decorator.decorate(new NotAuthedS3Client());

  for (int i = 0; i < 100; i++ ) {
    try {
      s3.getObjectMetadata("test-bucket", "test-key");
    } catch (AmazonServiceException e) {
      assertThat(e.getStatusCode()).isEqualTo(403);
    }

    Thread.sleep(50);
  }
}
 
Example 6
Project: s3-channels   File: S3RangedReadObjectChannel.java   View Source Code Vote up 4 votes
public S3RangedReadObjectChannel(String key, String bucket, AmazonS3 s3) {
    super(key, bucket, s3);
    metadata = s3.getObjectMetadata(bucket, key);
    size = metadata.getContentLength();
}
 
Example 7
Project: ibm-cos-sdk-java   File: ServiceUtils.java   View Source Code Vote up 3 votes
/**
 * Returns the part count of the object represented by the getObjectRequest.
 *
 * @param getObjectRequest
 * 					The request to check.
 * @param s3
 * 					The Amazon s3 client.
 *
 * @return  The number of parts in the object if it is multipart object, otherwise returns null.
 */
public static Integer getPartCount(GetObjectRequest getObjectRequest, AmazonS3 s3) {
    ValidationUtils.assertNotNull(s3, "S3 client");
    ValidationUtils.assertNotNull(getObjectRequest, "GetObjectRequest");

    ObjectMetadata metadata = s3.getObjectMetadata(new GetObjectMetadataRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey(), getObjectRequest.getVersionId())
            .withSSECustomerKey(getObjectRequest.getSSECustomerKey())
            .withPartNumber(1));
    return metadata.getPartCount();
}
 
Example 8
Project: ibm-cos-sdk-java   File: ServiceUtils.java   View Source Code Vote up 3 votes
/**
 * Returns the last byte number in a part of an object.
 *
 * @param s3
 *             The Amazon s3 client.
 * @param getObjectRequest
 *             The request to check.
 * @param partNumber
 *             The part in which we need the last byte number.
 * @return
 *         The last byte number in the part.
 */
public static long getLastByteInPart(AmazonS3 s3, GetObjectRequest getObjectRequest, Integer partNumber) {
    ValidationUtils.assertNotNull(s3, "S3 client");
    ValidationUtils.assertNotNull(getObjectRequest, "GetObjectRequest");
    ValidationUtils.assertNotNull(partNumber, "partNumber");

    ObjectMetadata metadata = s3.getObjectMetadata(new GetObjectMetadataRequest(getObjectRequest.getBucketName(), getObjectRequest.getKey(), getObjectRequest.getVersionId())
            .withSSECustomerKey(getObjectRequest.getSSECustomerKey())
            .withPartNumber(partNumber));
    return metadata.getContentRange()[1];
}