0

I am using the following solution below from another answer. However it will only send files of up to 64MB. How do I send a large file with this method? I could split the file in to pieces but then I can't reconstruct them once they are sent.

$accesskey = "qJXTMmw2Esal8/tXgfWk0RLwMNJ6OQKEt0E8EMZ8VhQrzGi5uqJqeKvi1l7iqnOddp7bdtai5rPpC6ynHttl1w==";$storageAccount = 'mystorage';
$filetoUpload = realpath('./image.jpg');

$containerName = '<yourblobcontainer>';
$blobName = 'image.jpg';

$destinationURL = "https://$storageAccount.blob.core.windows.net/$containerName/$blobName";

function uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey) {
$currentDate = gmdate("D, d M Y H:i:s T", time());
$handle = fopen($filetoUpload, "r");
$fileLen = filesize($filetoUpload);

$headerResource = "x-ms-blob-cache-control:max-age=3600\nx-ms-blob-type:BlockBlob\nx-ms-date:$currentDate\nx-ms-version:2015-12-11";
$urlResource = "/$storageAccount/$containerName/$blobName";

$arraysign = array();
$arraysign[] = 'PUT';               /*HTTP Verb*/  
$arraysign[] = '';                  /*Content-Encoding*/  
$arraysign[] = '';                  /*Content-Language*/  
$arraysign[] = $fileLen;            /*Content-Length (include value when zero)*/  
$arraysign[] = '';                  /*Content-MD5*/  
$arraysign[] = 'image/png';         /*Content-Type*/  
$arraysign[] = '';                  /*Date*/  
$arraysign[] = '';                  /*If-Modified-Since */  
$arraysign[] = '';                  /*If-Match*/  
$arraysign[] = '';                  /*If-None-Match*/  
$arraysign[] = '';                  /*If-Unmodified-Since*/  
$arraysign[] = '';                  /*Range*/  
$arraysign[] = $headerResource;     /*CanonicalizedHeaders*/
$arraysign[] = $urlResource;        /*CanonicalizedResource*/

$str2sign = implode("\n", $arraysign);

$sig = base64_encode(hash_hmac('sha256', urldecode(utf8_encode($str2sign)), base64_decode($accesskey), true));  
$authHeader = "SharedKey $storageAccount:$sig";

$headers = [
    'Authorization: ' . $authHeader,
    'x-ms-blob-cache-control: max-age=3600',
    'x-ms-blob-type: BlockBlob',
    'x-ms-date: ' . $currentDate,
    'x-ms-version: 2015-12-11',
    'Content-Type: image/png',
    'Content-Length: ' . $fileLen
];

$ch = curl_init($destinationURL);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_INFILE, $handle); 
curl_setopt($ch, CURLOPT_INFILESIZE, $fileLen); 
curl_setopt($ch, CURLOPT_UPLOAD, true); 
$result = curl_exec($ch);

echo ('Result<br/>');
print_r($result);

echo ('Error<br/>');
print_r(curl_error($ch));

curl_close($ch);
}

uploadBlob($filetoUpload, $storageAccount, $containerName, $blobName, $destinationURL, $accesskey);
1
  • Do you have any updates? Is it useful for you? if it is helpful for you, could you please accept it as an answer? It may help more people who have a similar issue. Commented Nov 12, 2020 at 9:05

1 Answer 1

1

If you want to upload large size file to Azure Blob Storage with PHP, you can use Azure PHP storage SDK.

For example

<?php

require_once "vendor/autoload.php"; 

use MicrosoftAzure\Storage\Blob\BlobRestProxy;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;

use MicrosoftAzure\Storage\Blob\Models\Block;
use MicrosoftAzure\Storage\Blob\Models\CommitBlobBlocksOptions;

define('CHUNK_SIZE', 1024*1024);//Block Size = 1 MB
try {

    $connectionString = "DefaultEndpointsProtocol=https;AccountName=andyprivate;AccountKey=h4pP1fe76m8hdksFW3TvkO6hgw09Mjue7yJOnULPI/g2eU8LGJ+a6k6SrU6dUkOU77waZfU8CacyVMlTWAUA5A==;EndpointSuffix=core.windows.net";
   
    $blobRestProxy = BlobRestProxy::createBlobService($connectionString);
    $containerName = "upload";
    $blobName = "psd7003.xml";
    $fileName="d:/download/psd7003.xml";
    $content = fopen("d:/download/psd7003.xml", "rb");
    $index = 0;
    $continue = True;
    $counter = 1;
    $blockIds = array();
    while (!feof($content))
    {
        $blockId = str_pad($counter, 6, "0", STR_PAD_LEFT);
        $block = new Block();
        $block -> setBlockId(base64_encode($blockId));
        $block -> setType("Uncommitted");
        array_push($blockIds, $block);
        echo $blockId . " | " . base64_encode($blockId) . " | " . count($blockIds);
        echo " \n ";
        echo " -----------------------------------------";
        $data=fread($content, CHUNK_SIZE);
        echo "Read " . strlen($data) . " of data from file";
        echo " \n ";
        echo " -----------------------------------------";
        echo " \n ";
        echo " -----------------------------------------";
        echo "Uploading block : " . $blockId + " into blob storage. Please wait.";
        echo " -----------------------------------------";
        echo " \n ";
        $blobRestProxy -> createBlobBlock($containerName, $blobName, base64_encode($blockId), $data);
        echo "Uploaded block: " . $blockId . " into blob storage. Please wait";
        echo " \n ";
        echo " -----------------------------------------";
        echo " \n ";
        $counter = $counter + 1;
    }
    fclose($content); 
    echo "Now committing block list. Please wait.";
    echo " -----------------------------------------";
    echo " \n ";
    echo "hello";
    $options = new CommitBlobBlocksOptions();
    $option -> setContentType(mime_content_type($fileName));
    $blobRestProxy -> commitBlobBlocks($containerName, $blobName, $blockIds,$options);
    echo " -----------------------------------------";
    echo " \n ";
    echo "Blob created successfully.";
}
catch(Exception $e){
    $code = $e->getCode();
    $error_message = $e->getMessage();
    echo $code.": ".$error_message."<br />";
}
?>

Besides, if you want to implement it with rest API, please refer to the following steps

1. Read the whole file to bytes, and divide the file into smaller pieces in your code.

  • Maybe 8 MB for each pieces.

2. Upload each piece with Put Block API.

  • In each request, it contains a blockid.

3. Make up the blob with Put Block List API.

  • In this request, you need to put all the blockid in the body in ordered.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.