使用 JavaScript 或 TypeScript 获取容器或 Blob 的 URL

可以使用客户端对象的 url 属性获取容器或 Blob URL:

备注

本文中的示例假定你已使用开始使用 Azure Blob 存储和 JavaScript 或 TypeScript 一文中的指导创建了一个 BlobServiceClient 对象。

获取容器或 Blob 的 URL

以下示例通过访问客户端的 url 属性获取容器 URL 和 Blob URL:

// 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}`);
}

提示

循环访问循环中的对象时,使用对象的 name 属性创建客户端,然后使用客户端获取 URL。 迭代器不返回客户端对象,它们返回项对象。

代码示例

另请参阅