Freigeben über


Abrufen einer URL für einen Container oder Blob mit JavaScript oder TypeScript

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

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 oder TypeScript erstellt haben.

Abrufen einer URL für einen Container oder 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

Verwenden Sie beim Durchlaufen von Objekten in einer Schleife die name-Eigenschaft des Objekts, um einen Client zu erstellen, und rufen Sie dann die URL mit dem Client ab. Iteratoren geben keine Clientobjekte, sondern Elementobjekte zurück.

Codebeispiele

Siehe auch