SDK für TypeScript und JavaScript mit Cloud Shell - Schnellstart
In diesem Schnellstart wird angezeigt, wie Sie schnell Beispielcode mit dem Oracle Cloud Infrastructure-SDK für TypeScript und JavaScript mit Cloud Shell ausführen.
Das OCI-SDK für TypeScript und JavaScript wird global über npm vorinstalliert.
JavaScript-Beispiel
- Melden Sie sich bei der Konsole an.
- Klicken Sie im Konsolenheader auf das Cloud Shell-Symbol. Beachten Sie, dass Cloud Shell Befehle für die Region ausführen, die beim Starten von Cloud Shell im Auswahlmenü "Region" der Konsole ausgewählt wurde.
- Erstellen Sie eine Datei namens
region_subscriptions_example.jsmit dem folgenden Beispielcode, in der die abonnierten Regionen für den aktuellen Mandanten aufgelistet sind:const identity = require("oci-sdk/node_modules/oci-identity"); const common = require("oci-sdk/node_modules/oci-common"); const provider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId() || ""; let identityClient; async function getSubscriptionRegions(tenancyId) { const regions = await identityClient.listRegionSubscriptions({ tenancyId: tenancyId }); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })(); - Erstellen Sie eine Datei namens
region_subscriptions_example.jsmit dem folgenden Beispielcode, in der die abonnierten Regionen für den aktuellen Mandanten aufgelistet sind:npm link oci-sdk - Führen Sie das Beispiel aus:
node region_subscriptions_example.js
TypeScript Beispiel
In diesem TypeScript-Beispiel werden die abonnierten Regionen für den aktuellen Mandanten aufgelistet.
- Erstellen Sie ein leeres Projekt mit dem Namen "ts_demo", und verknüpfen Sie die globale Installation der oci-sdk-Library mit den folgenden Befehlen:
# create a new project folder and move into it mkdir ts_demo cd ts_demo # initialize a new javascript/typescript project npm init --y # link the global installation of oci-sdk to the current project npm link oci-sdk npm link @types/node - erstellen Sie eine Datei namens
region_subscriptions_example.tsim Projekt ts_demo mit folgendem Code:import * as identity from "oci-sdk/node_modules/oci-identity"; import common = require("oci-sdk/node_modules/oci-common"); const provider: common.ConfigFileAuthenticationDetailsProvider = new common.ConfigFileAuthenticationDetailsProvider(); const compartmentId = provider.getTenantId(); let identityClient: identity.IdentityClient; export async function getSubscriptionRegions(tenancyId: string) { const listRegionSubscriptionsRequest: identity.requests.ListRegionSubscriptionsRequest = { tenancyId: tenancyId }; const regions = await identityClient.listRegionSubscriptions(listRegionSubscriptionsRequest); return regions.items.map(region => { return region.regionName; }); } (async () => { identityClient = await new identity.IdentityClient({ authenticationDetailsProvider: provider }); const regions = await getSubscriptionRegions(compartmentId); console.log("Currently subscribed to the following region(s): ", regions) })(); - Kompilieren Sie das Beispiel:
# use the TypeScript compiler to compile the example tsc region_subscriptions_example.ts - Führen Sie das Beispiel aus:
# run the example using node node region_subscriptions_example.js