You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently to create a Parse.File on Parse Server you need to upload the file directly one way or another which is a very processor , memory and bandwidth expensive operation, especially if you use an S3 adapter.
The file has to go from the client, to the parse server, and then to your storage provider
Feature / Enhancement Description
Providing upload urls is a common feature of many file storage providers, it allows us to not have to put the file through the parse server and directly from the client to the storage.
We can set up a Cloud Function that is used to generate an upload URL securely for the client, and then we should be able to create a Parse File in the client that has no data except the metadata, name and url of the file in the S3 storage.
Example Use Case
Here is my Cloud Function that gets an upload link from B2
// In your cloud code (cloud/main.js)Parse.Cloud.define('requestB2UploadUrls',async(request)=>{const{ user, params }=request;if(!user)thrownewError('You must be logged in to upload files');const{ numberOfFiles }=params;try{// Authorize with B2 to get the account authorization tokenconstauthResponse=awaitaxios.get('https://api.backblazeb2.com/b2api/v2/b2_authorize_account',{auth: {username: accountId,password: applicationKey}});constauthToken=authResponse.data.authorizationToken;constapiUrl=authResponse.data.apiUrl;// Get upload URLsconstuploadUrls=[];for(leti=0;i<numberOfFiles;i++){constuploadUrlResponse=awaitaxios({method: 'GET',url: `${apiUrl}/b2api/v2/b2_get_upload_url`,params: {
bucketId
},headers: {Authorization: authToken}});uploadUrls.push(uploadUrlResponse.data);}returnuploadUrls;}catch(error){thrownewParse.Error(500,error);}});
And here is how my client code should be
// Client-side function to get upload URLs from Parse ServerasyncfunctiongetUploadUrls(numberOfFiles){try{constuploadUrls=awaitParse.Cloud.run('requestB2UploadUrls',{ numberOfFiles });returnuploadUrls;}catch(error){console.error('Failed to get upload URLs:',error);}}/** Client-side function to upload a file to B2 and update Parse.File * @param {object} uploadUrlInfo * @param {Blob} fileBlob * @param {object} metadata * */asyncfunctionuploadFileToB2(uploadUrlInfo,fileBlob,metadata){try{// Upload the file blob to B2 using the upload URLconstresponse=awaitaxios.post(uploadUrlInfo.uploadUrl,fileBlob,{headers: {Authorization: uploadUrlInfo.authorizationToken,'X-Bz-File-Name': encodeURIComponent(fileBlob.name),'Content-Type': fileBlob.type,'X-Bz-Content-Sha1': 'do_not_verify',// You should calculate the SHA1 in a production app
...metadata}});// Update the Parse.File with the URL to the uploaded file on B2constb2FileUrl=`${b2UrlRoot}/${response.data.fileName}`;constparseFile=newParse.File(response.data.fileName,{url:b2FileUrl},fileBlob.type);parseFile.setMetadata(metadata);// Set the metadata on the Parse.File objectawaitparseFile.save();returnparseFile;}catch(error){console.error('Failed to upload file to B2:',error);}}
Alternatives / Workarounds
Not using Parse.File altogether but then we need to handle all of the deletion, metadata etc... on our own
We could also try to create file items using mongoose but this means we don't get afterFileSave or beforeFileSave functionality
3rd Party References
The text was updated successfully, but these errors were encountered:
New Feature / Enhancement Checklist
Current Limitation
Currently to create a Parse.File on Parse Server you need to upload the file directly one way or another which is a very processor , memory and bandwidth expensive operation, especially if you use an S3 adapter.
The file has to go from the client, to the parse server, and then to your storage provider
Feature / Enhancement Description
Providing upload urls is a common feature of many file storage providers, it allows us to not have to put the file through the parse server and directly from the client to the storage.
We can set up a Cloud Function that is used to generate an upload URL securely for the client, and then we should be able to create a Parse File in the client that has no data except the metadata, name and url of the file in the S3 storage.
Example Use Case
Here is my Cloud Function that gets an upload link from B2
And here is how my client code should be
Alternatives / Workarounds
Not using Parse.File altogether but then we need to handle all of the deletion, metadata etc... on our own
We could also try to create file items using mongoose but this means we don't get afterFileSave or beforeFileSave functionality
3rd Party References
The text was updated successfully, but these errors were encountered: