Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.0k views
in Technique[技术] by (71.8m points)

microsoft graph api - how to upload a large file to onedrive in php on the backend

I need any help regarding this problem, I have done the research and I found it confusing. I need to upload a file in the backend in PHP this will be automated. the idea is that I send 2 parameters 1-subfolder name and 2-the actual file path and in return, I get the file link after upload.

also, all the work needs to be done in the folder of a subfolder (eg:all this even searching and creating subfolders will be inside a folder, say videos for example) with a given name, if it does not exist then create it and insert the given file there and return the link any help is much appreciated

so far i created the app in azure got credentials - used composer on the folder where i will use my project (https://github.com/microsoftgraph/msgraph-sdk-php)

and this is my code so far:

<?php

    include_once __DIR__ . '/vendor/autoload.php';

        $tenantId = '***ddfb62';    
        $clientId = 'b**d1453fde';
        $clientSecret = '***l2Em6';
        $guzzle = new GuzzleHttpClient();
        $url = 'https://login.microsoftonline.com/' . $tenantId . '/oauth2/token?api-version=1.0';
        $token = json_decode($guzzle->post($url, [
            'form_params' => [
                'client_id' => $clientId,
                'client_secret' => $clientSecret,
                'resource' => 'https://graph.microsoft.com/',
                'grant_type' => 'client_credentials',
            ],
        ])->getBody()->getContents());
        $accessToken = $token->access_token;


        var_dump ($accessToken);





        /** @var ModelUploadSession $uploadSession */
        $uploadSession = $graph->createRequest("POST", "/me/drive/items/root:/doc-test2.docx:/createUploadSession")
            ->addHeaders(["Content-Type" => "application/json"])
            ->attachBody([
                "item" => [
                    "@microsoft.graph.conflictBehavior" => "rename",
                    "description"    => 'File description here'
                ]
            ])
            ->setReturnType(ModelUploadSession::class)
            ->execute();

        $file = __DIR__.'./downloads/beztam.mp4';
        $handle = fopen($file, 'r');
        $fileSize = fileSize($file);
        $fileNbByte = $fileSize - 1;
        $chunkSize = 1024*1024*4;
        $fgetsLength = $chunkSize + 1;
        $start = 0;
        while (!feof($handle)) {
            $bytes = fread($handle, $fgetsLength);
            $end = $chunkSize + $start;
            if ($end > $fileNbByte) {
                $end = $fileNbByte;
            }
            /* or use stream
            $stream = GuzzleHttpPsr7stream_for($bytes);
            */
            $res = $graph->createRequest("PUT", $uploadSession->getUploadUrl())
                ->addHeaders([
                    'Content-Length' => ($end - 1) - $start,
                    'Content-Range' => "bytes " . $start . "-" . $end . "/" . $fileSize
                ])
                ->setReturnType(ModelUploadSession::class)
                ->attachBody($bytes)
                ->execute();

            $start = $end + 1;
        }

UPDATE SO after debugging i am now getting SPO LICENSE ERROR ?? so ireaserched found i need to pay to work ? is there any way to upload without it ? i have another acc with office but can't access azure as it is not administrator this is the error i am getting

PHP Fatal error:  Uncaught GuzzleHttpExceptionClientException: Client error: `POST https://graph.microsoft.com/v1.0/me/drive/items/root:/doc-test2.docx:/createUploadSession` resulted in a `400 Bad Request` response:
{
  "error": {
    "code": "BadRequest",
    "message": "Tenant does not have a SPO license.",
    "innerError": {
 (truncated...)
question from:https://stackoverflow.com/questions/65933642/how-to-upload-a-large-file-to-onedrive-in-php-on-the-backend

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...