Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@
import software.amazon.awssdk.services.s3.model.GetBucketLifecycleConfigurationResponse;
import software.amazon.awssdk.services.s3.model.GetBucketTaggingRequest;
import software.amazon.awssdk.services.s3.model.GetBucketTaggingResponse;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesRequest;
import software.amazon.awssdk.services.s3.model.GetObjectAttributesResponse;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.services.s3.model.GetObjectResponse;
import software.amazon.awssdk.services.s3.model.GetObjectTaggingRequest;
Expand All @@ -160,6 +162,7 @@
import software.amazon.awssdk.services.s3.model.ListPartsRequest;
import software.amazon.awssdk.services.s3.model.MetadataDirective;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import software.amazon.awssdk.services.s3.model.ObjectAttributes;
import software.amazon.awssdk.services.s3.model.ObjectIdentifier;
import software.amazon.awssdk.services.s3.model.PutBucketAclRequest;
import software.amazon.awssdk.services.s3.model.PutBucketTaggingRequest;
Expand Down Expand Up @@ -1712,6 +1715,93 @@ public void testHeadObjectReturnsTaggingCount() {
assertEquals(tags.size(), head.tagCount().intValue());
}

@Test
public void testGetObjectAttributes() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
final String content = "get-object-attributes-content";
s3Client.createBucket(b -> b.bucket(bucketName));

PutObjectResponse putObjectResponse = s3Client.putObject(b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

GetObjectAttributesResponse attributesResponse = s3Client.getObjectAttributes(
GetObjectAttributesRequest.builder()
.bucket(bucketName)
.key(keyName)
.objectAttributes(ObjectAttributes.E_TAG, ObjectAttributes.OBJECT_SIZE,
ObjectAttributes.STORAGE_CLASS)
.build());

assertNotNull(attributesResponse.lastModified());
assertEquals(
putObjectResponse.eTag().replace("\"", ""),
attributesResponse.eTag());
assertEquals((long) content.length(), attributesResponse.objectSize());
assertEquals("STANDARD", attributesResponse.storageClassAsString());
assertNull(attributesResponse.objectParts());
}

@Test
public void testGetObjectAttributesMultipartObjectParts(@TempDir Path tempDir) throws Exception {
final String bucketName = getBucketName();
final String keyName = getKeyName();

s3Client.createBucket(b -> b.bucket(bucketName));

File multipartUploadFile = Files.createFile(tempDir.resolve("get-object-attributes-mpu.txt")).toFile();
createFile(multipartUploadFile, (int) (15 * MB));
multipartUpload(bucketName, keyName, multipartUploadFile, (int) (5 * MB), new HashMap<>(),
Collections.emptyList());

GetObjectAttributesResponse attributesResponse = s3Client.getObjectAttributes(
GetObjectAttributesRequest.builder()
.bucket(bucketName)
.key(keyName)
.objectAttributes(ObjectAttributes.OBJECT_PARTS, ObjectAttributes.E_TAG,
ObjectAttributes.OBJECT_SIZE)
.build());

assertNotNull(attributesResponse.eTag());
assertTrue(attributesResponse.eTag().contains("-"));
assertEquals(multipartUploadFile.length(), attributesResponse.objectSize());
assertNotNull(attributesResponse.objectParts());
assertEquals(3, attributesResponse.objectParts().totalPartsCount());
assertFalse(attributesResponse.objectParts().isTruncated());
}

@Test
public void testGetObjectAttributesNoSuchKey() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
s3Client.createBucket(b -> b.bucket(bucketName));

assertThrows(NoSuchKeyException.class, () -> s3Client.getObjectAttributes(
GetObjectAttributesRequest.builder()
.bucket(bucketName)
.key(keyName)
.objectAttributes(ObjectAttributes.E_TAG)
.build()));
}

@Test
public void testGetObjectAttributesChecksumOmitted() {
final String bucketName = getBucketName();
final String keyName = getKeyName();
s3Client.createBucket(b -> b.bucket(bucketName));
s3Client.putObject(b -> b.bucket(bucketName).key(keyName), RequestBody.fromString("checksum-test"));

GetObjectAttributesResponse attributesResponse = s3Client.getObjectAttributes(
GetObjectAttributesRequest.builder()
.bucket(bucketName)
.key(keyName)
.objectAttributes(ObjectAttributes.CHECKSUM, ObjectAttributes.E_TAG)
.build());

assertNotNull(attributesResponse.eTag());
assertNull(attributesResponse.checksum());
}

@Test
public void testResumableDownloadWithEtagMismatch() throws Exception {
// Arrange
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ public enum S3GAction implements AuditAction {
GET_BUCKET_TAGGING,
PUT_BUCKET_TAGGING,
DELETE_BUCKET_TAGGING,
PUT_OBJECT_ACL;
PUT_OBJECT_ACL,
GET_OBJECT_ATTRIBUTES;

@Override
public String getAction() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import static org.apache.hadoop.ozone.OzoneConsts.KB;
import static org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_CLIENT_BUFFER_SIZE_DEFAULT;
import static org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_CLIENT_BUFFER_SIZE_KEY;
import static org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_FSO_DIRECTORY_CREATION_ENABLED;
import static org.apache.hadoop.ozone.s3.S3GatewayConfigKeys.OZONE_S3G_FSO_DIRECTORY_CREATION_ENABLED_DEFAULT;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.BUCKET_ALREADY_EXISTS;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.BUCKET_ALREADY_OWNED_BY_YOU;
import static org.apache.hadoop.ozone.s3.exception.S3ErrorTable.INVALID_ARGUMENT;
Expand Down Expand Up @@ -810,6 +812,23 @@ protected static CheckedRunnable<IOException> validateContentLength(
};
}

/**
* Rejects FSO directory keys when the request path omits the trailing slash.
*
* <p>Necessary for directories in buckets with FSO layout. Intended for apps which use Hadoop S3A
* (e.g. Trino through the Hive connector).
*/
protected void validateFileKey(String keyPath, OzoneKey key) throws OMException {
boolean isFsoDirCreationEnabled = getOzoneConfiguration()
.getBoolean(OZONE_S3G_FSO_DIRECTORY_CREATION_ENABLED,
OZONE_S3G_FSO_DIRECTORY_CREATION_ENABLED_DEFAULT);
if (isFsoDirCreationEnabled &&
!key.isFile() &&
!keyPath.endsWith("/")) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can key.isFile() return true for the key ends with '/'?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No — for FSO directory keys, isFile() is false. A file key has isFile()==true. the trailing-slash check only gates directory access without /.

throw new OMException(ResultCodes.KEY_NOT_FOUND);
}
}

protected static String extractPartsCount(String eTag) {
if (eTag.contains("-")) {
String[] parts = eTag.replace("\"", "").split("-");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.ozone.s3.endpoint;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import org.apache.hadoop.ozone.s3.util.S3Consts;

/**
* XML response for the GetObjectAttributes S3 API.
*
* <p>See https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetObjectAttributes.html
*
* <p>The {@code Checksum} field is intentionally omitted: Ozone does not yet store
* non-MD5 checksum algorithms in key metadata. When checksum storage is implemented,
* this field can be added here without any API contract change.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "GetObjectAttributesResponse", namespace = S3Consts.S3_XML_NAMESPACE)
public class GetObjectAttributesResponse {

@XmlElement(name = "ETag")
private String eTag;

@XmlElement(name = "ObjectSize")
private Long objectSize;

@XmlElement(name = "StorageClass")
private String storageClass;

@XmlElement(name = "ObjectParts")
private ObjectParts objectParts;

public String getETag() {
return eTag;
}

public void setETag(String tag) {
this.eTag = tag;
}

public Long getObjectSize() {
return objectSize;
}

public void setObjectSize(Long objectSize) {
this.objectSize = objectSize;
}

public String getStorageClass() {
return storageClass;
}

public void setStorageClass(String storageClass) {
this.storageClass = storageClass;
}

public ObjectParts getObjectParts() {
return objectParts;
}

public void setObjectParts(ObjectParts objectParts) {
this.objectParts = objectParts;
}

/**
* Represents the ObjectParts element in the GetObjectAttributes response.
*
* <p>For completed multipart-uploaded objects, {@code partsCount} is derived from
* the composite ETag suffix (e.g. {@code "hash-15"} → 15 parts). Per-part sizes
* are not stored for completed multipart uploads in Ozone and are therefore omitted
* from the part list in this response.
* TODO: Will support completed multipart uploads in this ticket: HDDS-16073
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "ObjectParts")
public static class ObjectParts {

@XmlElement(name = "IsTruncated")
private boolean truncated;

@XmlElement(name = "MaxParts")
private Integer maxParts;

@XmlElement(name = "PartNumberMarker")
private Integer partNumberMarker;

@XmlElement(name = "NextPartNumberMarker")
private Integer nextPartNumberMarker;

@XmlElement(name = "PartsCount")
private Integer partsCount;

@XmlElement(name = "Part")
private List<Part> parts = new ArrayList<>();

public boolean isTruncated() {
return truncated;
}

public void setTruncated(boolean truncated) {
this.truncated = truncated;
}

public Integer getMaxParts() {
return maxParts;
}

public void setMaxParts(Integer maxParts) {
this.maxParts = maxParts;
}

public Integer getPartNumberMarker() {
return partNumberMarker;
}

public void setPartNumberMarker(Integer partNumberMarker) {
this.partNumberMarker = partNumberMarker;
}

public Integer getNextPartNumberMarker() {
return nextPartNumberMarker;
}

public void setNextPartNumberMarker(Integer nextPartNumberMarker) {
this.nextPartNumberMarker = nextPartNumberMarker;
}

public Integer getPartsCount() {
return partsCount;
}

public void setPartsCount(Integer partsCount) {
this.partsCount = partsCount;
}

public List<Part> getParts() {
return parts;
}

public void setParts(List<Part> parts) {
this.parts = parts;
}

public void addPart(Part part) {
this.parts.add(part);
}
}

/**
* A single part entry within {@link ObjectParts}.
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Part")
public static class Part {

@XmlElement(name = "PartNumber")
private int partNumber;

@XmlElement(name = "Size")
private long size;

public Part() {
}

public Part(int partNumber, long size) {
this.partNumber = partNumber;
this.size = size;
}

public int getPartNumber() {
return partNumber;
}

public void setPartNumber(int partNumber) {
this.partNumber = partNumber;
}

public long getSize() {
return size;
}

public void setSize(long size) {
this.size = size;
}
}
}
Loading