List Azure files and folders from File share snapshots with php

183 views Asked by At

To list the files and folders from Azure Files can be done with this code:

function ListFolder($shareName, $path)
{
    global $fileRestProxy;
    $vMaxResultados = 5000;
    $vNextMarker = "";
    $listResult = null;
    try
    {
        do
        {
            $options = new ListDirectoriesAndFilesOptions();
            $options->setMaxResults($vMaxResultados);
            $options->setMarker($vNextMarker);
            $listResult = $fileRestProxy->ListDirectoriesAndFiles($shareName,$path,$options);
            $vNextMarker = $listResult->getNextMarker();
        } while ($vNextMarker != "");
    }
    catch (Exception $e)
    {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        return "ERROR:$code:$error_message";
    }
    return $listResult;
}

But how is the sintaxis or method to the same with a snapshot from these Share?

This doesn't work:

function ListSnapshotFolder($shareName, $path, $snapshot)
{
    global $fileRestProxy;
    $vMaxResultados = 5000;
    $vNextMarker = "";
    $listResult = null;
    try
    {
        do
        {
            $options = new ListDirectoriesAndFilesOptions();
            $options->setMaxResults($vMaxResultados);
            $options->setMarker($vNextMarker);
            $shareFull = $shareName . "?snapshot=" . $snapshot; 
            $listResult = $fileRestProxy->ListDirectoriesAndFiles($shareFull,$path,$options);
            $vNextMarker = $listResult->getNextMarker();
        } while ($vNextMarker != "");
    }
    catch (Exception $e)
    {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        return "ERROR:$code:$error_message";
    }
    return $listResult;
}

Is there any parameter in the $option object to add? Or maybe the $shareFull must be created in some format? $shareFull = $shareName . "?snapshot=" . $snapshot;

Thanks in advance.

1

There are 1 answers

1
Gaurav Mantri On

I believe you have found a bug in the SDK. I looked up the source code here and there's no provision to provide sharesnapshot query string parameter in the options as well as the code does not even handle it.

    public function listDirectoriesAndFilesAsync(
        $share,
        $path = '',
        ListDirectoriesAndFilesOptions $options = null
    ) {
        Validate::notNull($share, 'share');
        Validate::canCastAsString($share, 'share');
        Validate::canCastAsString($path, 'path');

        $method      = Resources::HTTP_GET;
        $headers     = array();
        $postParams  = array();
        $queryParams = array();
        $path        = $this->createPath($share, $path);

        if (is_null($options)) {
            $options = new ListDirectoriesAndFilesOptions();
        }

        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_REST_TYPE,
            'directory'
        );
        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_COMP,
            'list'
        );
        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_PREFIX_LOWERCASE,
            $options->getPrefix()
        );
        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_MARKER_LOWERCASE,
            $options->getNextMarker()
        );
        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_MAX_RESULTS_LOWERCASE,
            $options->getMaxResults()
        );

        $this->addOptionalQueryParam(
            $queryParams,
            Resources::QP_TIMEOUT,
            $options->getTimeout()
        );

        $dataSerializer = $this->dataSerializer;

        return $this->sendAsync(
            $method,
            $headers,
            $queryParams,
            $postParams,
            $path,
            Resources::STATUS_OK,
            Resources::EMPTY_STRING,
            $options
        )->then(function ($response) use ($dataSerializer) {
            $parsed = $dataSerializer->unserialize($response->getBody());
            return ListDirectoriesAndFilesResult::create(
                $parsed,
                Utilities::getLocationFromHeaders($response->getHeaders())
            );
        }, null);
    }

You may want to open up an issue here: https://github.com/Azure/azure-storage-php/issues and bring this to SDK team's attention.