// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.storage.blob;

import com.azure.storage.common.StorageSharedKeyCredential;

import java.util.Locale;

/**
 * This example shows how to list all containers with storage client using the Azure Storage Blob SDK for Java.
 */
public class ListContainersExample {

    /**
     * Entry point into the list containers examples for Storage blobs.
     * @param args Unused. Arguments to the program.
     */
    public static void main(String[] args) {
        String accountName = SampleHelper.getAccountName();
        String accountKey = SampleHelper.getAccountKey();

        /*
         * Use your Storage account's name and key to create a credential object; this is used to access your account.
         */
        StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);

        /*
         * From the Azure portal, get your Storage account blob service URL endpoint.
         * The URL typically looks like this:
         */
        String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);

        /*
         * Create a BlobServiceClient object that wraps the service endpoint, credential and a request pipeline.
         */
        BlobServiceClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).buildClient();

        /*
         * Create 3 different containers from the storageClient.
         */
        for (int i = 0; i < 3; i++) {
            storageClient.createBlobContainer("mycontainersforlisting" + i + System.currentTimeMillis());
        }

        /*
         * List the containers' name under the Azure storage account.
         */
        storageClient.listBlobContainers().forEach(containerItem -> {
            System.out.println("Container name: " + containerItem.getName());

            /*
             * Clean up the containers at the same time.
             */
            storageClient.getBlobContainerClient(containerItem.getName()).delete();
        });
    }
}
