-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Storage] Async iterator for listing blobs and containers - storage-b…
…lob (#3136) * Async iterator for container - listBlobs * remove iter in the generator * Add marker in the generator * yield -> yield * * Add sample using `.next().value` * containerClient - *listContainers * udpdate - remove resumable from comment * Addressed comments * Add description for options attribute * remove process.env * un-specify the type Models.xxx * remove !options * rectify indentation * listBlobs -> listBlobsFlat * Add test for ListContainers * remove .only * Add test for listBlobsFlat * Add test case for listBlobsFlat - for loop syntax * Add test case for listContainers - for loop syntax
- Loading branch information
1 parent
81c4cd6
commit 70de2aa
Showing
5 changed files
with
340 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Setup: Enter your storage account name and shared key in main() | ||
*/ | ||
|
||
import { | ||
ContainerClient, | ||
BlobServiceClient, | ||
StorageClient, | ||
SharedKeyCredential, | ||
BlobClient, | ||
BlockBlobClient | ||
} from "../../src"; // Change to "@azure/storage-blob" in your package | ||
|
||
async function main() { | ||
// Enter your storage account name and shared key | ||
const account = ""; | ||
const accountKey = ""; | ||
|
||
// Use SharedKeyCredential with storage account and account key | ||
const sharedKeyCredential = new SharedKeyCredential(account, accountKey); | ||
|
||
// Use sharedKeyCredential, tokenCredential or anonymousCredential to create a pipeline | ||
const pipeline = StorageClient.newPipeline(sharedKeyCredential); | ||
|
||
// List containers | ||
const blobServiceClient = new BlobServiceClient( | ||
// When using AnonymousCredential, following url should include a valid SAS or support public access | ||
`https://${account}.blob.core.windows.net`, | ||
pipeline | ||
); | ||
|
||
// List Containers | ||
let iter1 = await blobServiceClient.listContainers(); | ||
let i = 1; | ||
for await (const container of iter1) { | ||
console.log(`Container ${i++}: ${container.name}`); | ||
} | ||
|
||
// List containers - generator syntax | ||
let iter2 = await blobServiceClient.listContainers(); | ||
i = 1; | ||
let containerItem = await iter2.next(); | ||
do { | ||
console.log(`Container ${i++}: ${containerItem.value.name}`); | ||
containerItem = await iter2.next(); | ||
} while (containerItem.value); | ||
|
||
// Create a container | ||
const containerName = `newcontainer${new Date().getTime()}`; | ||
const containerClient = ContainerClient.fromBlobServiceClient(blobServiceClient, containerName); | ||
|
||
const createContainerResponse = await containerClient.create(); | ||
console.log(`Created container ${containerName} successfully`, createContainerResponse.requestId); | ||
|
||
for (let index = 0; index < 4; index++) { | ||
// Create a blob | ||
let content = "hello"; | ||
let blobName = "newblob" + new Date().getTime(); | ||
let blobClient = BlobClient.fromContainerClient(containerClient, blobName); | ||
let blockBlobClient = BlockBlobClient.fromBlobClient(blobClient); | ||
let uploadBlobResponse = await blockBlobClient.upload(content, content.length); | ||
console.log(`Uploaded block blob ${blobName} successfully`, uploadBlobResponse.requestId); | ||
} | ||
|
||
// List blobs | ||
iter1 = await containerClient.listBlobsFlat(); | ||
i = 1; | ||
for await (const blob of iter1) { | ||
console.log(`Blob ${i++}: ${blob.name}`); | ||
} | ||
|
||
// List blobs - generator syntax | ||
iter2 = await containerClient.listBlobsFlat(); | ||
i = 1; | ||
let blobItem = await iter2.next(); | ||
do { | ||
console.log(`Blob ${i++}: ${blobItem.value.name}`); | ||
blobItem = await iter2.next(); | ||
} while (blobItem.value); | ||
} | ||
|
||
// An async method returns a Promise object, which is compatible with then().catch() coding style. | ||
main() | ||
.then(() => { | ||
console.log("Successfully executed the sample."); | ||
}) | ||
.catch((err) => { | ||
console.log(err.message); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.