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 @@ -1032,6 +1032,62 @@ public void testPresignedUrlGet() throws IOException {
}
}

/**
* Tests the functionality to create a snapshot of an Ozone bucket and then read files
* from the snapshot directory using the S3 SDK.
*
* <p>The test follows these steps:
* <ol>
* <li>Create a bucket and upload a file via the S3 client.</li>
* <li>Create a snapshot on the bucket using the Ozone client.</li>
* <li>Construct the snapshot object key using the ".snapshot" directory format.</li>
* <li>Retrieve the object from the snapshot and verify that its content matches
* the originally uploaded content.</li>
* </ol>
* </p>
*
* @throws Exception if the test fails due to any errors during bucket creation, snapshot creation,
* file upload, or retrieval.
*/
@Test
public void testReadSnapshotDirectoryUsingS3SDK() throws Exception {
final String bucketName = getBucketName("snapshot");
final String keyName = getKeyName("snapshotfile");
final String content = "snapshot test content";
final byte[] contentBytes = content.getBytes(StandardCharsets.UTF_8);

// Create the bucket and upload an object using S3 SDK.
InputStream is = new ByteArrayInputStream(content.getBytes(StandardCharsets.UTF_8));
s3Client.createBucket(bucketName);
s3Client.putObject(bucketName, keyName,
is, new ObjectMetadata());

String snapshotName = "snap1";
// Create a snapshot using the Ozone client.
// Snapshots in Ozone are created on the bucket and are exposed via the ".snapshot" directory.
try (OzoneClient ozoneClient = cluster.newClient()) {
ObjectStore store = ozoneClient.getObjectStore();
OzoneVolume volume = store.getS3Volume();

store.createSnapshot(volume.getName(), bucketName, snapshotName);
}

// Use the S3 SDK to read the file from the snapshot directory.
// The key in the snapshot is constructed using the special ".snapshot" prefix.
String snapshotKey = ".snapshot/" + snapshotName + "/" + keyName;

S3Object s3Object = s3Client.getObject(bucketName, snapshotKey);
try (S3ObjectInputStream s3is = s3Object.getObjectContent();
ByteArrayOutputStream bos = new ByteArrayOutputStream(contentBytes.length)) {
byte[] readBuf = new byte[1024];
int readLen = 0;
while ((readLen = s3is.read(readBuf)) > 0) {
bos.write(readBuf, 0, readLen);
}
assertEquals(content, bos.toString("UTF-8"));
}
}

private boolean isBucketEmpty(Bucket bucket) {
ObjectListing objectListing = s3Client.listObjects(bucket.getName());
return objectListing.getObjectSummaries().isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,53 @@ public void testPresignedUrlGet() throws Exception {
}
}

/**
* Tests the functionality to create a snapshot of an Ozone bucket and then read files
* from the snapshot directory using the S3 SDK.
*
* <p>The test follows these steps:
* <ol>
* <li>Create a bucket and upload a file via the S3 client.</li>
* <li>Create a snapshot on the bucket using the Ozone client.</li>
* <li>Construct the snapshot object key using the ".snapshot" directory format.</li>
* <li>Retrieve the object from the snapshot and verify that its content matches
* the originally uploaded content.</li>
* </ol>
* </p>
*
* @throws Exception if the test fails due to any errors during bucket creation, snapshot creation,
* file upload, or retrieval.
*/
@Test
public void testReadSnapshotDirectoryUsingS3SDK() throws Exception {
final String bucketName = getBucketName("snapshot");
final String keyName = getKeyName("snapshotfile");
final String content = "snapshot test content";

// Create the bucket and upload an object using S3 SDK.
s3Client.createBucket(b -> b.bucket(bucketName));
s3Client.putObject(b -> b.bucket(bucketName).key(keyName),
RequestBody.fromString(content));

String snapshotName = "snap1";
// Create a snapshot using the Ozone client.
// Snapshots in Ozone are created on the bucket and are exposed via the ".snapshot" directory.
try (OzoneClient ozoneClient = cluster.newClient()) {
ObjectStore store = ozoneClient.getObjectStore();
OzoneVolume volume = store.getS3Volume();

store.createSnapshot(volume.getName(), bucketName, snapshotName);
}

// Use the S3 SDK to read the file from the snapshot directory.
// The key in the snapshot is constructed using the special ".snapshot" prefix.
String snapshotKey = ".snapshot/" + snapshotName + "/" + keyName;
ResponseBytes<GetObjectResponse> snapshotResponse = s3Client.getObjectAsBytes(
b -> b.bucket(bucketName).key(snapshotKey));

assertEquals(content, snapshotResponse.asUtf8String());
}

private String getBucketName() {
return getBucketName("");
}
Expand Down