Abrufen der URL für Container oder Blob mit JavaScript

Sie können eine Container- oder Blob-URL mithilfe der Eigenschaft url des Clientobjekts abrufen:

  • ContainerClient.url
  • BlobClient.url
  • BlockBlobClient.url

Die Beispielcodeausschnitte sind in GitHub als ausführbare Node.js-Dateien verfügbar.

Hinweis

In den Beispielen in diesem Artikel wird davon ausgegangen, dass Sie ein BlobServiceClient-Objekt anhand der Anleitung im Artikel Erste Schritte mit Azure Blob Storage und JavaScript erstellt haben.

Abrufen einer URL für Container und Blob

Das folgende Beispiel ruft eine Container-URL und eine Blob-URL ab, indem auf die Eigenschaft url des Clients zugegriffen wird:


// create container
const containerName = `con1-${Date.now()}`;
const { containerClient } = await blobServiceClient.createContainer(containerName, {access: 'container'});

// Display container name and its URL
console.log(`created container:\n\tname=${containerClient.containerName}\n\turl=${containerClient.url}`);

// create blob from string
const blobName = `${containerName}-from-string.txt`;
const blobContent = `Hello from a string`;
const blockBlobClient = await containerClient.getBlockBlobClient(blobName);
await blockBlobClient.upload(blobContent, blobContent.length);

// Display Blob name and its URL 
console.log(`created blob:\n\tname=${blobName}\n\turl=${blockBlobClient.url}`);

// In loops, blob is BlobItem
// Use BlobItem.name to get BlobClient or BlockBlobClient
// The get `url` property
for await (const blob of containerClient.listBlobsFlat()) {
    
    // blob 
    console.log("\t", blob.name);

    // Get Blob Client from name, to get the URL
    const tempBlockBlobClient = containerClient.getBlockBlobClient(blob.name);

    // Display blob name and URL
    console.log(`\t${blob.name}:\n\t\t${tempBlockBlobClient.url}`);
}

Tipp

Bei Schleifen müssen Sie die Eigenschaft name des Objekts verwenden, um einen Client zu erstellen und dann die URL mit dem Client abzurufen. Iteratoren geben keine Clientobjekte, sondern Elementobjekte zurück.

Siehe auch