software.amazon.awssdk.services.s3.model.HeadObjectRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.s3.model.HeadObjectRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UserMetadataIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void putObject_PutsUserMetadata() throws Exception {
    Map<String, String> userMetadata = new HashMap<>();
    userMetadata.put("thing1", "IAmThing1");
    userMetadata.put("thing2", "IAmThing2");

    String mixedCasePrefix = "x-AmZ-mEtA-";
    String metadataKey = "test";

    final String key = "user-metadata-key";
    s3.putObject(PutObjectRequest.builder()
                                 .bucket(BUCKET_NAME)
                                 .key(key)
                                 .metadata(userMetadata)
                                 .overrideConfiguration(b -> b.putHeader(mixedCasePrefix + metadataKey, "test"))
                                 .build(),
                 RequestBody.fromFile(file));

    HeadObjectResponse response = s3.headObject(HeadObjectRequest.builder()
                                                                 .bucket(BUCKET_NAME)
                                                                 .key(key)
                                                                 .build());

    Map<String, String> returnedMetadata = response.metadata();

    assertThat(returnedMetadata).containsAllEntriesOf(userMetadata);
    assertThat(returnedMetadata).containsKey(metadataKey);
}
 
Example #2
Source File: CopyObjectIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that the simple form of the copy object operation correctly copies
 * an object.
 */
@Test
public void copyObject_CopiesObjectToNewKey() throws Exception {
    s3.copyObject(CopyObjectRequest.builder()
                                   .copySource(BUCKET_NAME + "/" + SOURCE_KEY)
                                   .bucket(BUCKET_NAME)
                                   .key(DESTINATION_KEY)
                                   .build());

    s3.headObject(HeadObjectRequest.builder()
                                   .bucket(BUCKET_NAME)
                                   .key(DESTINATION_KEY)
                                   .build());
}
 
Example #3
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headObject404_shouldTranslateException() {
    S3Exception s3Exception = create404S3Exception();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadObjectRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes()))
        .isExactlyInstanceOf(NoSuchKeyException.class);
}
 
Example #4
Source File: ExceptionTranslationInterceptorTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void headObjectOtherException_shouldNotThrowException() {
    S3Exception s3Exception = (S3Exception) S3Exception.builder().statusCode(500).build();
    Context.FailedExecution failedExecution = getFailedExecution(s3Exception,
                                                                 HeadObjectRequest.builder().build());

    assertThat(interceptor.modifyException(failedExecution, new ExecutionAttributes())).isEqualTo(s3Exception);
}
 
Example #5
Source File: S3PinotFS.java    From incubator-pinot with Apache License 2.0 5 votes vote down vote up
private HeadObjectResponse getS3ObjectMetadata(URI uri)
    throws IOException {
  URI base = getBase(uri);
  String path = sanitizePath(base.relativize(uri).getPath());
  HeadObjectRequest headObjectRequest = HeadObjectRequest.builder().bucket(uri.getHost()).key(path).build();

  return _s3Client.headObject(headObjectRequest);
}
 
Example #6
Source File: S3RandomAccessFile.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private S3RandomAccessFile(String url) throws IOException {
  super(url, s3BufferSize, s3MaxReadCacheSize);

  // Region is tricky. Since we are using AWS SDK to manage connections to all object stores, we might have users
  // who use netCDF-Java and never touch AWS. If that's they case, they likely have not setup a basic credentials or
  // configuration file, and thus lack a default region. What we will do here is check to see if there is one set.
  // If, by the time we make the client, profileRegion isn't set, we will default to the AWS_GLOBAL region, which is
  // like a no-op region when it comes to S3. This will allow requests to non-AWS-S3 object stores to work, because
  // a region must be set, even if it's useless.
  Optional<Region> profileRegion = ProfileFile.defaultProfileFile().profile("default")
      .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);

  try {
    uri = new CdmS3Uri(url);
  } catch (URISyntaxException urie) {
    // If we are given a string that is not a valid CdmS3Uri
    // throw an IOException
    throw new IOException(urie.getCause());
  }

  Builder httpConfig = ApacheHttpClient.builder().maxConnections(maxConnections)
      .connectionTimeout(Duration.ofMillis(connectionTimeout)).socketTimeout(Duration.ofMillis(socketTimeout));

  S3ClientBuilder s3ClientBuilder = S3Client.builder().httpClientBuilder(httpConfig);

  // if we are accessing an S3 compatible service, we need to override the server endpoint
  uri.getEndpoint().ifPresent(s3ClientBuilder::endpointOverride);

  // build up a chain of credentials providers
  AwsCredentialsProviderChain.Builder cdmCredentialsProviderChainBuilder = AwsCredentialsProviderChain.builder();

  // if uri has a profile name, we need setup a credentials provider to look for potential credentials, and see if a
  // region has been set
  if (uri.getProfile().isPresent()) {
    // get the profile name
    String profileName = uri.getProfile().get();

    ProfileCredentialsProvider namedProfileCredentials =
        ProfileCredentialsProvider.builder().profileName(profileName).build();

    // add it to the chain that it is the first thing checked for credentials
    cdmCredentialsProviderChainBuilder.addCredentialsProvider(namedProfileCredentials);

    // Read the region associated with the profile, if set
    // Note: the java sdk does not do this by default
    Optional<Region> namedProfileRegion = ProfileFile.defaultProfileFile().profile(profileName)
        .map(p -> p.properties().get(ProfileProperty.REGION)).map(Region::of);
    // if the named profile has a region, update profileRegion to use it.
    if (namedProfileRegion.isPresent()) {
      profileRegion = namedProfileRegion;
    }
  }

  // Add the Default Credentials Provider Chain:
  // https://docs.aws.amazon.com/sdk-for-java/v2/developer-guide/credentials.html
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(DefaultCredentialsProvider.create());

  // Add the AnonymousCredentialsProvider last
  cdmCredentialsProviderChainBuilder.addCredentialsProvider(AnonymousCredentialsProvider.create());

  // build the credentials provider that we'll use
  AwsCredentialsProviderChain cdmCredentialsProviderChain = cdmCredentialsProviderChainBuilder.build();

  // Add the credentials provider to the client builder
  s3ClientBuilder.credentialsProvider(cdmCredentialsProviderChain);

  // Set the region for the client builder (default to AWS_GLOBAL)
  s3ClientBuilder.region(profileRegion.orElse(Region.AWS_GLOBAL));

  // Build the client
  client = s3ClientBuilder.build();

  // request HEAD for the object
  HeadObjectRequest headdObjectRequest =
      HeadObjectRequest.builder().bucket(uri.getBucket()).key(uri.getKey()).build();

  objectHeadResponse = client.headObject(headdObjectRequest);
}
 
Example #7
Source File: ExceptionTranslationInterceptor.java    From aws-sdk-java-v2 with Apache License 2.0 4 votes vote down vote up
private boolean isHeadRequest(SdkRequest request) {
    return (request instanceof HeadObjectRequest || request instanceof HeadBucketRequest);
}
 
Example #8
Source File: S3TestUtils.java    From incubator-pinot with Apache License 2.0 4 votes vote down vote up
public static HeadObjectRequest getHeadObjectRequest(String bucket, String key) {
  return HeadObjectRequest.builder().bucket(bucket).key(key).build();
}