Skip to content

Commit f571253

Browse files
authored
Avoid HEAD request before getting blobs (#525)
2 parents aa7e1df + f731aca commit f571253

2 files changed

Lines changed: 21 additions & 10 deletions

File tree

src/main/java/land/oras/Registry.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ public Manifest pushArtifact(
354354
public Layer pushBlob(ContainerRef containerRef, Path blob, Map<String, String> annotations) {
355355
String digest = containerRef.getAlgorithm().digest(blob);
356356
LOG.debug("Digest: {}", digest);
357+
// This migh not works with registries performing HEAD request
357358
if (hasBlob(containerRef.withDigest(digest))) {
358359
LOG.info("Blob already exists: {}", digest);
359360
return Layer.fromFile(blob, containerRef.getAlgorithm()).withAnnotations(annotations);
@@ -491,9 +492,6 @@ private HttpClient.ResponseWrapper<String> headBlob(ContainerRef containerRef) {
491492
*/
492493
@Override
493494
public byte[] getBlob(ContainerRef containerRef) {
494-
if (!hasBlob(containerRef)) {
495-
throw new OrasException(new HttpClient.ResponseWrapper<>("", 404, Map.of()));
496-
}
497495
URI uri = URI.create(
498496
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getBlobsPath(this)));
499497
HttpClient.ResponseWrapper<String> response = client.get(
@@ -510,9 +508,6 @@ public byte[] getBlob(ContainerRef containerRef) {
510508

511509
@Override
512510
public void fetchBlob(ContainerRef containerRef, Path path) {
513-
if (!hasBlob(containerRef)) {
514-
throw new OrasException(new HttpClient.ResponseWrapper<>("", 404, Map.of()));
515-
}
516511
URI uri = URI.create(
517512
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getBlobsPath(this)));
518513
HttpClient.ResponseWrapper<Path> response = client.download(
@@ -528,9 +523,6 @@ public void fetchBlob(ContainerRef containerRef, Path path) {
528523

529524
@Override
530525
public InputStream fetchBlob(ContainerRef containerRef) {
531-
if (!hasBlob(containerRef)) {
532-
throw new OrasException(new HttpClient.ResponseWrapper<>("", 404, Map.of()));
533-
}
534526
URI uri = URI.create(
535527
"%s://%s".formatted(getScheme(), containerRef.forRegistry(this).getBlobsPath(this)));
536528
HttpClient.ResponseWrapper<InputStream> response = client.download(

src/test/java/land/oras/PublicECRITCase.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@
2222

2323
import static org.junit.jupiter.api.Assertions.assertNotNull;
2424

25+
import org.junit.jupiter.api.Disabled;
2526
import org.junit.jupiter.api.Test;
2627
import org.junit.jupiter.api.parallel.Execution;
2728
import org.junit.jupiter.api.parallel.ExecutionMode;
2829

29-
@Execution(ExecutionMode.CONCURRENT)
30+
@Execution(ExecutionMode.SAME_THREAD) // Avoid 429 Too Many Requests for unauthenticated requests to public ECR
3031
class PublicECRITCase {
3132

3233
@Test
@@ -44,4 +45,22 @@ void shouldPullAnonymousIndex() {
4445
Index index2 = registry.getIndex(containerRef2);
4546
assertNotNull(index2);
4647
}
48+
49+
@Test
50+
@Disabled("https://github.com/oras-project/oras-java/issues/522")
51+
void shouldPullManifest() {
52+
Registry registry = Registry.builder().build();
53+
ContainerRef containerRef = ContainerRef.parse(
54+
"public.ecr.aws/docker/library/alpine@sha256:59855d3dceb3ae53991193bd03301e082b2a7faa56a514b03527ae0ec2ce3a95");
55+
Manifest manifest = registry.getManifest(containerRef);
56+
assertNotNull(manifest);
57+
}
58+
59+
@Test
60+
void shouldPullLayer() {
61+
Registry registry = Registry.builder().build();
62+
ContainerRef containerRef = ContainerRef.parse(
63+
"public.ecr.aws/docker/library/alpine@sha256:589002ba0eaed121a1dbf42f6648f29e5be55d5c8a6ee0f8eaa0285cc21ac153");
64+
registry.getBlob(containerRef);
65+
}
4766
}

0 commit comments

Comments
 (0)