'ms graph upload session stream and response drive item
Requirement: upload a large file (4MB+) using MSGraph (onedrive or Sharepoint) and return its corresponding DriveID (i.e. DriveItem)
This example uses PHP but I assume it's common due to the endpoint response.
Prerequisites:
- MS Registered App with correct permissions
- Valid auth token
Overview of large file uploads: 2 calls at a minimum required.
- Create Upload Session Request
- Use Session to upload chuncks of data
Using the PHP sdk https://github.com/microsoftgraph/msgraph-sdk-php
From point 1 our response is \Microsoft\Graph\Model\UploadSession
From point 2 (within a loop while chuncks < filesize) response is also \Microsoft\Graph\Model\UploadSession but with some additional information
Once the upload completes these are the results that come out of the UploadSession Object
object(Microsoft\Graph\Model\UploadSession)#274 (1) {
["_propDict":protected]=>
array(16) {
["@odata.context"]=>
string(79) "https://xxxx.sharepoint.com/sites/EH_SHARED/_api/v2.0/$metadata#items/$entity"
["@content.downloadUrl"]=>
string(1692) "https://xxxx.sharepoint.com/sites/EH_SHARED/_layouts/15/download.aspx?UniqueId=6b41a4a1-bb7c-4abb-991a-8ff743ea7271&T... zlJTjdQcktWR2I2Y0JRWFlFaDVmVi9tUklIVT0&ApiVersion=2.0"
["createdBy"]=>
array(2) {
["application"]=>
array(2) {
["id"]=>
string(36) "17ea34aa-...-786eb4146c"
["displayName"]=>
string(12) "Test App"
}
["user"]=>
array(3) {
["email"]=>
string(36) "adminin...microsoft.com"
["id"]=>
string(36) "baaeb-...-1ce24ca191"
["displayName"]=>
string(13) "DN"
}
}
["createdDateTime"]=>
string(20) "2022-04-22T03:44:49Z"
["eTag"]=>
string(42) ""{6B41A4A1-...-43EA7271},4""
["id"]=>
string(34) "01IX5T2WN'..'JSGUP65B6U4TR"
["lastModifiedBy"]=>
array(2) {
["application"]=>
array(2) {
["id"]=>
string(36) "17eaa...-9eb4146c"
["displayName"]=>
string(12) "Test App"
}
["user"]=>
array(3) {
["email"]=>
string(36) "[email protected]"
["id"]=>
string(36) "baaeb-...-ce24ca191"
["displayName"]=>
string(13) "DN"
}
}
["lastModifiedDateTime"]=>
string(20) "2022-04-22T03:44:50Z"
["name"]=>
string(20) "Firewall-1.png"
["parentReference"]=>
array(4) {
["driveType"]=>
string(15) "documentLibrary"
["driveId"]=>
string(66) "b!LH33a_4...PHrdpRILuxk1T-EvS"
["id"]=>
string(34) "01IX5T2....F5YQ4CK7WGZC"
["path"]=>
string(127) "/drives/b!LH33a_4wiEu0A4FKQ6j...MEu_PHrdpRILuxk1T-EvS/root:/Clients/CompanyA/JN00001 - Test Project/images"
}
["webUrl"]=>
string(132) "https://xxxx.sharepoint.com/sites/EH_SHARED/TEST%20Files/Clients/CompanyA/JN00001%20-%20Test%20Project/images/Firewall-1.png"
["cTag"]=>
string(44) ""c:{6B41A-....-43EA7271},4""
["file"]=>
array(3) {
["hashes"]=>
array(1) {
["quickXorHash"]=>
string(28) "uI9XNv6......s3e5/rHic="
}
["irmEnabled"]=>
bool(false)
["mimeType"]=>
string(9) "image/png"
}
["fileSystemInfo"]=>
array(2) {
["createdDateTime"]=>
string(20) "2022-04-22T03:44:49Z"
["lastModifiedDateTime"]=>
string(20) "2022-04-22T03:44:50Z"
}
["image"]=>
array(0) {
}
["size"]=>
int(27721)
}
}
The only part of this that references the newly uploaded document is the 'webUrl'
$response->getProperties()
["webUrl"]=>
string(132) "https://xxxx.sharepoint.com/sites/EH_SHARED/TEST%20Files/Clients/CompanyA/JN00001%20-%20Test%20Project/images/Firewall-1.png"
So, with the above information how can one retrieve the DriveID of the file that has just been uploaded? I've tried use MSGraph API as a get request on the webURL which returns a "401 Unauthorized" error.
Solution 1:[1]
Answer: Return type of Graph call must be of \Microsoft\Graph\Model\DriveItem
$returnType = \Microsoft\Graph\Model\DriveItem::class;
$result = $graph->createRequest("PUT", $graph_url)
->addHeaders($headers)
->attachBody($data)
->setReturnType($returnType)
->setTimeout("1000")
->execute();
This will return the DriveItem and allow the retrieval of DriveId. NOTE: I've continued to use
$returnType = \Microsoft\Graph\Model\UploadSession::class;
and only switched to DriveItem on the last loop ($bytes = $chuncksize) Now just to catch a curl upload error when uploading large files.... another post likely
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | DharmanBot |
