Is there a way to programmatically (PHP) generate Azure SaS token for File share?

Carlos G 0 Reputation points
2023-08-08T09:38:36.78+00:00

On our server we need to generate SAS token for File share. Hence we need it in this format.

?sv=2022-11-02&ss=b&srt=o&sp=rwdlaciytfx&se=2023-08-08T17:28:10Z&st=2023-08-08T09:28:10Z&spr=https&sig=[SIGNATURE]

I could not find anything realted to File Storage only Blob.

Thank you!

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,420 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
{count} votes

1 answer

Sort by: Most helpful
  1. deherman-MSFT 38,021 Reputation points Microsoft Employee Moderator
    2023-08-08T21:29:04.9833333+00:00

    @Carlos G

    The Azure Storage PHP client libraries will be retired on 17 March 2024. I was able to find a sample of how to do this programmatically for Azure Files. Please check and let me know if this works for you.

        // Create a SharedAccessSignatureHelper
        global $connectionString;
    
        $settings = StorageServiceSettings::createFromConnectionString($connectionString);
        $accountName = $settings->getName();
        $accountKey = $settings->getKey();
    
        $helper = new FileSharedAccessSignatureHelper(
            $accountName,
            $accountKey
        );
    
        // Generate a file readonly SAS token
        // Refer to following link for full candidate values to construct a service level SAS
        // https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-a-service-sas
        $sas = $helper->generateFileServiceSharedAccessSignatureToken(
            Resources::RESOURCE_TYPE_FILE,
            "$shareName/$fileName",
            'r',                        // Read
            '2030-01-01T08:30:00Z'      // A valid ISO 8601 format expiry time
        );
    
        $connectionStringWithSAS = Resources::FILE_ENDPOINT_NAME .
            '='.
            'https://' .
            $accountName .
            '.' .
            Resources::FILE_BASE_DNS_NAME .
            ';' .
            Resources::SAS_TOKEN_NAME .
            '=' .
            $sas;
    
        $fileClientWithSAS = FileRestProxy::createFileService(
            $connectionStringWithSAS
        );
    
        // Get a downloadable file URL
        $fileUrlWithSAS = sprintf(
            '%s%s?%s',
            (string)$fileClientWithSAS->getPsrPrimaryUri(),
            "$shareName/$fileName",
            $sas
        );
    

    Hope this helps! Let me know if you have any questions.


    If you still have questions, please let us know in the "comments" and we would be happy to help you. Comment is the fastest way of notifying the experts.

    If the answer has been helpful, we appreciate hearing from you and would love to help others who may have the same question. Accepting answers helps increase visibility of this question for other members of the Microsoft Q&A community.

    Thank you for helping to improve Microsoft Q&A! User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.