msgraph-sdk-javascript icon indicating copy to clipboard operation
msgraph-sdk-javascript copied to clipboard

Uploading files using OneDriveLargeFileUploadTask with only the Files.ReadWrite.AppFolder permission is not possible

Open chjohan opened this issue 3 years ago • 6 comments

Bug Report

Prerequisites

  • [x] Can you reproduce the problem?
  • [x] Are you running the latest version?
  • [x] Are you reporting to the correct repository?
  • [x] Did you perform a cursory search?

For more information, see the CONTRIBUTING guide.

Description

The name of the "Apps"-folder which is used when using the "Files.ReadWrite.AppFolder" permission is determined from the users' language. For english users the folder is named "Apps", bur for norwegian users the folder is named "Apper". As a consuquence it is very inpractical uploading files with "OneDriveLargeFileUploadTask", which requires you to supply the upload path when creating an upload session because, there is no way to get the name of the "Apps"-folder using only the "Files.ReadWrite.AppFolder"-permission. For this you have to enable the Files.Read permission.

Console Errors: Not applicable.

Screenshots: [If applicable, add screenshots to help explain your problem]

Steps to Reproduce

  1. Create an Azure AD appregistration with the following delegated permissions:
  • User.Read
  • Files.ReadWrite.AppFolder
  • offline_access
  1. Grant permissions to the app from a test user
  2. Upload a large file
const uploadTask = await OneDriveLargeFileUploadTask.create(
	graphClient, fileContent, {
	path: "/Apps/MyApp",
	fileName: fileName,
		rangeSize: 4 * 1024 * 1024, // must be a multiple of 320 KiB
		conflictBehavior: "fail"
	}
);
const response = await uploadTask.upload();

Expected behavior: I either should be able to upload the file without knowing the name of the users "Apps" folder, or be allowed to get the name of the "Apps" folder, with the "Files.ReadWrite.AppFolder" permission, or be able to provide a path using the special/approot path format.

Actual behavior: "Apps"-folder does not exist when the target user has a different language than english, resulting in a 404.

Usage Information

Request ID - Value of the requestId field if you are receiving a Graph API error response

SDK Version - "@microsoft/microsoft-graph-client": "^3.0.2",

  • [x] Node (Check, if using Node version of SDK)

Node Version - v12

  • [ ] Browser (Check, if using Browser version of SDK)

Browser Name - Not applicable. Server side.

Version - [The version of the browser you are using]

chjohan avatar Aug 09 '22 09:08 chjohan

@chjohan This is an interesting use case that you have brought forward. Thank you!

nikithauc avatar Aug 09 '22 20:08 nikithauc

@chjohan Are you using a personal authentication token or a business/org account? That is, OneDrive for consumer or OneDrive for business?

nikithauc avatar Aug 11 '22 08:08 nikithauc

@chjohan Are you using a personal authentication token or a business/org account? That is, OneDrive for consumer or OneDrive for business?

@nikithauc Personal authentication token. OneDrive for consumer. I have not tested with a business/org account.

chjohan avatar Aug 11 '22 17:08 chjohan

Hi @chjohan , I am from the API team (not the SDK team) - the functionality you want is available by combining 'special folder' addressing with the 'resumable upload' API: https://docs.microsoft.com/en-us/graph/api/drive-get-specialfolder?view=graph-rest-1.0&tabs=http https://docs.microsoft.com/en-us/graph/api/driveitem-createuploadsession?view=graph-rest-1.0

The combined request would look something like: POST https://graph.microsoft.com/v1.0/me/drive/special/approot:/somePath/fileToCreate.txt:/createUploadSession

I am not very familiar with the SDK code, but looking at OneDriveLargeFileUploadTask.create(), it looks like it hardcodes the API path to something like /me/drive/root:/{file-path}:/createUploadSession, which will prevent you from using the 'special folder' addressing method.

It seems like to work around this you'd need to replicate the logic in create() and createTaskWithFileObject(), but modify createTaskWithFileObject to use your own custom request URI instead of calling constructCreateSessionUrl().

@nikithauc - it would probably be helpful to include a way in the SDK to compose your own request URI here.

Thanks, Kevin

kevklam avatar Aug 12 '22 17:08 kevklam

Thank you so much for your help @kevklam!

@chjohan You can use the LargeFileTaskUpload instead which will help you to upload the file to OneDrive and you can customize the path. https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/tasks/LargeFileUploadTask.md.

Please let me know if you have more questions.

nikithauc avatar Aug 12 '22 21:08 nikithauc

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment.

ghost avatar Aug 17 '22 02:08 ghost

Fix released in v3.0.3

nikithauc avatar Nov 03 '22 22:11 nikithauc