magicianiam
magicianiam

Reputation: 1579

how to copy/upload a locally created file to azure file storage using PHP

I'm trying to follow this link: https://learn.microsoft.com/en-us/rest/api/storageservices/copy-file

with examples from this repo: https://github.com/Azure/azure-storage-php/blob/master/samples/FileSamples.php#L235

The file is indeed copied to the azure server but the content aren't readable, to say the least, it takes a size but it's empty. This is only a text file as well, and what I plan to achieve after fixing this is to copy excel files generated via PHP to an azure file storage server.

Also, we are using file.core not blob.core

<?php

    require_once "vendor/autoload.php"; 
    use MicrosoftAzure\Storage\File\FileRestProxy;
    use MicrosoftAzure\Storage\Common\Models\Range;

    $accountName = "test";
    $accountKey = "test";

    $shareName = 'test';

    $connectionString = "DefaultEndpointsProtocol=https;AccountName=$accountName;AccountKey=$accountKey";
    $fileClient = FileRestProxy::createFileService($connectionString);

    $dstfileName = 'demo-4.txt';
    $srcfileName = 'demo-4.txt';

    $sourcePath = sprintf(
        '%s%s/%s',
        (string)$fileClient->getPsrPrimaryUri(),
        $shareName,
        $srcfileName
    );

    try {
        // Create destination file.
        $fileClient->createFile($shareName, $dstfileName, 1024);
        // Copy file.
        return $fileClient->copyFile($shareName, $dstfileName, $sourcePath);
    } catch (ServiceException $e) {
        $code = $e->getCode();
        $error_message = $e->getMessage();
        echo $code . ": " . $error_message . PHP_EOL;
    }

Update using file_get_contents

$srcfileName = 'demo-4.txt';

$content = file_get_contents('demo-4.txt');

$range = new Range(0, filesize('demo-4.txt') - 1);
$sourcePath = sprintf(
    '%s%s/%s',
    (string)$fileClient->getPsrPrimaryUri(),
    $shareName,
    $srcfileName
);

try {
    // Create source file.
    $fileClient->createFile($shareName, $srcfileName, 1024);

    $fileClient->putFileRange($shareName, $srcfileName, $content, $range);

} catch (ServiceException $e) {
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code . ": " . $error_message . PHP_EOL;
}

This is able to create the file with the content from the source file, but the problem is that the range is incorrect since I don't know how to correctly get that value.

enter image description here

The created file is presented by the image attached, it has multiple nulls in it because I'm guessing my range exceeds the actual length of the source file contents.

Upvotes: 0

Views: 1343

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136126

createFile method simply creates an empty file of size specified in the method call. It essentially maps to Create File REST API operation.

You should use createFileFromContent convenience method to create a file with content. It basically first creates an empty file and then writes the contents to that file.

Other option would be to call putFileRange method to write the contents to the file after you have created it using createFile method.

Upvotes: 1

Related Questions