Skip to content

Commit 070afb9

Browse files
authored
[ACR] Samples for deleteBlob, deleteManifest, and custom manifest upload/download (Azure#25238)
1 parent f32e42d commit 070afb9

File tree

14 files changed

+527
-0
lines changed

14 files changed

+527
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes the blobs associated with a given manifest from the repository.
6+
* @azsdk-weight 3
7+
*/
8+
9+
import {
10+
ContainerRegistryBlobClient,
11+
isDownloadOciImageManifestResult,
12+
} from "@azure/container-registry";
13+
import { DefaultAzureCredential } from "@azure/identity";
14+
import * as dotenv from "dotenv";
15+
dotenv.config();
16+
17+
async function main() {
18+
// Get the service endpoint from the environment
19+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
20+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
21+
// Create a new ContainerRegistryClient
22+
const client = new ContainerRegistryBlobClient(
23+
endpoint,
24+
repository,
25+
new DefaultAzureCredential()
26+
);
27+
28+
const downloadResult = await client.downloadManifest("latest");
29+
30+
if (!isDownloadOciImageManifestResult(downloadResult)) {
31+
throw new Error("Expected an OCI image manifest");
32+
}
33+
34+
for (const layer of downloadResult.manifest.layers) {
35+
await client.deleteBlob(layer.digest);
36+
}
37+
}
38+
39+
main().catch((err) => {
40+
console.error("The sample encountered an error:", err);
41+
});
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes a given manifest from the repository.
6+
* @azsdk-weight 3
7+
*/
8+
9+
import { ContainerRegistryBlobClient } from "@azure/container-registry";
10+
import { DefaultAzureCredential } from "@azure/identity";
11+
import * as dotenv from "dotenv";
12+
dotenv.config();
13+
14+
async function main() {
15+
// Get the service endpoint from the environment
16+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
17+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
18+
// Create a new ContainerRegistryClient
19+
const client = new ContainerRegistryBlobClient(
20+
endpoint,
21+
repository,
22+
new DefaultAzureCredential()
23+
);
24+
25+
const downloadResult = await client.downloadManifest("latest");
26+
await client.deleteManifest(downloadResult.digest);
27+
}
28+
29+
main().catch((err) => {
30+
console.error("The sample encountered an error:", err);
31+
});
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Downloads a manifest of custom media type.
6+
* @azsdk-weight 3
7+
*/
8+
9+
import { ContainerRegistryBlobClient } from "@azure/container-registry";
10+
import { DefaultAzureCredential } from "@azure/identity";
11+
import * as dotenv from "dotenv";
12+
dotenv.config();
13+
14+
async function main() {
15+
// endpoint should be in the form of "https://myregistryname.azurecr.io"
16+
// where "myregistryname" is the actual name of your registry
17+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
18+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
19+
const client = new ContainerRegistryBlobClient(
20+
endpoint,
21+
repository,
22+
new DefaultAzureCredential()
23+
);
24+
25+
const manifestListType = "application/vnd.docker.distribution.manifest.list.v2+json";
26+
const ociIndexType = "application/vnd.oci.image.index.v1+json";
27+
28+
const result = await client.downloadManifest("latest", {
29+
mediaType: [manifestListType, ociIndexType],
30+
});
31+
32+
if (result.mediaType === manifestListType) {
33+
console.log("Manifest is a Docker manifest list");
34+
} else if (result.mediaType === ociIndexType) {
35+
console.log("Manifest is an OCI index");
36+
}
37+
}
38+
39+
main().catch((err) => {
40+
console.error("The sample encountered an error:", err);
41+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Uploads a manifest with custom manifest type, in this case a manifest list.
6+
* @azsdk-weight 3
7+
*/
8+
9+
import { ContainerRegistryBlobClient } from "@azure/container-registry";
10+
import { DefaultAzureCredential } from "@azure/identity";
11+
import * as dotenv from "dotenv";
12+
import { Readable } from "stream";
13+
dotenv.config();
14+
15+
async function main() {
16+
// endpoint should be in the form of "https://myregistryname.azurecr.io"
17+
// where "myregistryname" is the actual name of your registry
18+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
19+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
20+
const client = new ContainerRegistryBlobClient(
21+
endpoint,
22+
repository,
23+
new DefaultAzureCredential()
24+
);
25+
26+
const mediaType = "application/vnd.docker.distribution.manifest.list.v2+json";
27+
28+
const manifest = Readable.from(
29+
JSON.stringify({
30+
schemaVersion: 2,
31+
mediaType,
32+
manifests: [
33+
{
34+
mediaType: "application/vnd.docker.distribution.manifest.v2+json",
35+
digest: "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
36+
size: 7143,
37+
platform: {
38+
architecture: "ppc64le",
39+
os: "linux",
40+
},
41+
},
42+
{
43+
mediaType: "application/vnd.docker.distribution.manifest.v2+json",
44+
digest: "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
45+
size: 7682,
46+
platform: {
47+
architecture: "amd64",
48+
os: "linux",
49+
features: ["sse4"],
50+
},
51+
},
52+
],
53+
})
54+
);
55+
56+
await client.uploadManifest(manifest, { mediaType });
57+
}
58+
59+
main().catch((err) => {
60+
console.error("The sample encountered an error:", err);
61+
});

sdk/containerregistry/container-registry/samples/v1-beta/javascript/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ These sample programs show how to use the JavaScript client libraries for Azure
1919
| [deleteImages.js][deleteimages] | Deletes all but the latest three images. |
2020
| [repositoryAndArtifact.js][repositoryandartifact] | Uses ContainerRepository and RegistryArtifact to work with manifests, tags, and artifacts. |
2121
| [setImageProperties.js][setimageproperties] | Updates the properties on the tag so it can't be overwritten or deleted. |
22+
| [deleteBlob.js][deleteblob] | Deletes the blobs associated with a given manifest from the repository. |
23+
| [deleteManifest.js][deletemanifest] | Deletes a given manifest from the repository. |
24+
| [downloadCustomManifest.js][downloadcustommanifest] | Downloads a manifest of custom media type. |
2225
| [downloadImage.js][downloadimage] | Downloads an image from the repository. |
26+
| [uploadCustomManifest.js][uploadcustommanifest] | Uploads a manifest with custom manifest type, in this case a manifest list. |
2327
| [uploadImage.js][uploadimage] | Uploads an image to the repository. |
2428
| [uploadManifest.js][uploadmanifest] | Uploads a manifest to a repository. |
2529

@@ -68,7 +72,11 @@ Take a look at our [API Documentation][apiref] for more information about the AP
6872
[deleteimages]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/deleteImages.js
6973
[repositoryandartifact]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/repositoryAndArtifact.js
7074
[setimageproperties]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/setImageProperties.js
75+
[deleteblob]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/deleteBlob.js
76+
[deletemanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/deleteManifest.js
77+
[downloadcustommanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/downloadCustomManifest.js
7178
[downloadimage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/downloadImage.js
79+
[uploadcustommanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/uploadCustomManifest.js
7280
[uploadimage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/uploadImage.js
7381
[uploadmanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/javascript/uploadManifest.js
7482
[apiref]: https://docs.microsoft.com/javascript/api/@azure/container-registry
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes the blobs associated with a given manifest from the repository.
6+
*/
7+
8+
const {
9+
ContainerRegistryBlobClient,
10+
isDownloadOciImageManifestResult,
11+
} = require("@azure/container-registry");
12+
const { DefaultAzureCredential } = require("@azure/identity");
13+
require("dotenv").config();
14+
15+
async function main() {
16+
// Get the service endpoint from the environment
17+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
18+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
19+
// Create a new ContainerRegistryClient
20+
const client = new ContainerRegistryBlobClient(
21+
endpoint,
22+
repository,
23+
new DefaultAzureCredential()
24+
);
25+
26+
const downloadResult = await client.downloadManifest("latest");
27+
28+
if (!isDownloadOciImageManifestResult(downloadResult)) {
29+
throw new Error("Expected an OCI image manifest");
30+
}
31+
32+
for (const layer of downloadResult.manifest.layers) {
33+
await client.deleteBlob(layer.digest);
34+
}
35+
}
36+
37+
main().catch((err) => {
38+
console.error("The sample encountered an error:", err);
39+
});
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Deletes a given manifest from the repository.
6+
*/
7+
8+
const { ContainerRegistryBlobClient } = require("@azure/container-registry");
9+
const { DefaultAzureCredential } = require("@azure/identity");
10+
require("dotenv").config();
11+
12+
async function main() {
13+
// Get the service endpoint from the environment
14+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
15+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
16+
// Create a new ContainerRegistryClient
17+
const client = new ContainerRegistryBlobClient(
18+
endpoint,
19+
repository,
20+
new DefaultAzureCredential()
21+
);
22+
23+
const downloadResult = await client.downloadManifest("latest");
24+
await client.deleteManifest(downloadResult.digest);
25+
}
26+
27+
main().catch((err) => {
28+
console.error("The sample encountered an error:", err);
29+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Downloads a manifest of custom media type.
6+
*/
7+
8+
const { ContainerRegistryBlobClient } = require("@azure/container-registry");
9+
const { DefaultAzureCredential } = require("@azure/identity");
10+
require("dotenv").config();
11+
12+
async function main() {
13+
// endpoint should be in the form of "https://myregistryname.azurecr.io"
14+
// where "myregistryname" is the actual name of your registry
15+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
16+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
17+
const client = new ContainerRegistryBlobClient(
18+
endpoint,
19+
repository,
20+
new DefaultAzureCredential()
21+
);
22+
23+
const manifestListType = "application/vnd.docker.distribution.manifest.list.v2+json";
24+
const ociIndexType = "application/vnd.oci.image.index.v1+json";
25+
26+
const result = await client.downloadManifest("latest", {
27+
mediaType: [manifestListType, ociIndexType],
28+
});
29+
30+
if (result.mediaType === manifestListType) {
31+
console.log("Manifest is a Docker manifest list");
32+
} else if (result.mediaType === ociIndexType) {
33+
console.log("Manifest is an OCI index");
34+
}
35+
}
36+
37+
main().catch((err) => {
38+
console.error("The sample encountered an error:", err);
39+
});
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* @summary Uploads a manifest with custom manifest type, in this case a manifest list.
6+
*/
7+
8+
const { ContainerRegistryBlobClient } = require("@azure/container-registry");
9+
const { DefaultAzureCredential } = require("@azure/identity");
10+
const dotenv = require("dotenv");
11+
const { Readable } = require("stream");
12+
dotenv.config();
13+
14+
async function main() {
15+
// endpoint should be in the form of "https://myregistryname.azurecr.io"
16+
// where "myregistryname" is the actual name of your registry
17+
const endpoint = process.env.CONTAINER_REGISTRY_ENDPOINT || "<endpoint>";
18+
const repository = process.env.CONTAINER_REGISTRY_REPOSITORY || "library/hello-world";
19+
const client = new ContainerRegistryBlobClient(
20+
endpoint,
21+
repository,
22+
new DefaultAzureCredential()
23+
);
24+
25+
const mediaType = "application/vnd.docker.distribution.manifest.list.v2+json";
26+
27+
const manifest = Readable.from(
28+
JSON.stringify({
29+
schemaVersion: 2,
30+
mediaType,
31+
manifests: [
32+
{
33+
mediaType: "application/vnd.docker.distribution.manifest.v2+json",
34+
digest: "sha256:e692418e4cbaf90ca69d05a66403747baa33ee08806650b51fab815ad7fc331f",
35+
size: 7143,
36+
platform: {
37+
architecture: "ppc64le",
38+
os: "linux",
39+
},
40+
},
41+
{
42+
mediaType: "application/vnd.docker.distribution.manifest.v2+json",
43+
digest: "sha256:5b0bcabd1ed22e9fb1310cf6c2dec7cdef19f0ad69efa1f392e94a4333501270",
44+
size: 7682,
45+
platform: {
46+
architecture: "amd64",
47+
os: "linux",
48+
features: ["sse4"],
49+
},
50+
},
51+
],
52+
})
53+
);
54+
55+
await client.uploadManifest(manifest, { mediaType });
56+
}
57+
58+
main().catch((err) => {
59+
console.error("The sample encountered an error:", err);
60+
});

sdk/containerregistry/container-registry/samples/v1-beta/typescript/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ These sample programs show how to use the TypeScript client libraries for Azure
1919
| [deleteImages.ts][deleteimages] | Deletes all but the latest three images. |
2020
| [repositoryAndArtifact.ts][repositoryandartifact] | Uses ContainerRepository and RegistryArtifact to work with manifests, tags, and artifacts. |
2121
| [setImageProperties.ts][setimageproperties] | Updates the properties on the tag so it can't be overwritten or deleted. |
22+
| [deleteBlob.ts][deleteblob] | Deletes the blobs associated with a given manifest from the repository. |
23+
| [deleteManifest.ts][deletemanifest] | Deletes a given manifest from the repository. |
24+
| [downloadCustomManifest.ts][downloadcustommanifest] | Downloads a manifest of custom media type. |
2225
| [downloadImage.ts][downloadimage] | Downloads an image from the repository. |
26+
| [uploadCustomManifest.ts][uploadcustommanifest] | Uploads a manifest with custom manifest type, in this case a manifest list. |
2327
| [uploadImage.ts][uploadimage] | Uploads an image to the repository. |
2428
| [uploadManifest.ts][uploadmanifest] | Uploads a manifest to a repository. |
2529

@@ -80,7 +84,11 @@ Take a look at our [API Documentation][apiref] for more information about the AP
8084
[deleteimages]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/deleteImages.ts
8185
[repositoryandartifact]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/repositoryAndArtifact.ts
8286
[setimageproperties]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/setImageProperties.ts
87+
[deleteblob]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/deleteBlob.ts
88+
[deletemanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/deleteManifest.ts
89+
[downloadcustommanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/downloadCustomManifest.ts
8390
[downloadimage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/downloadImage.ts
91+
[uploadcustommanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/uploadCustomManifest.ts
8492
[uploadimage]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/uploadImage.ts
8593
[uploadmanifest]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/containerregistry/container-registry/samples/v1-beta/typescript/src/uploadManifest.ts
8694
[apiref]: https://docs.microsoft.com/javascript/api/@azure/container-registry

0 commit comments

Comments
 (0)