次の方法で共有


JavaScript または TypeScript を使用してコンテナーまたは BLOB の URL を取得する

コンテナーまたは BLOB URL は、クライアント オブジェクトの url プロパティを使用して取得できます。

Note

この記事の例では、「Azure Blob Storage と 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 を取得する必要があります。 反復子はクライアント オブジェクトを返さず、項目オブジェクトを返します。

コード サンプル

関連項目