Start a new topic

NativeScript file upload to Kinvey error

Hello!


I'm making app in NativeScript (JavaScript) with Kinvey backend. Everything works fine except uploading image file (camera module) to Kinvey (Google Cloud Storage).

 

The code I'm uploading file with is:


camera.takePicture()
.then(function (imageAsset) {
console.log("Result is an image asset instance");
console.dir(imageAsset);
//var image = new imageModule.Image();
//image.src = imageAsset;
console.log('Uploading image');

var fileContent = fs.File.fromPath(imageAsset.android);
fileContent.readText()
.then(function (content) {

var metadata = {
filename: '12222.jpg',
mimeType: 'image/jpeg',
size: content.length,
public: true
};
var promise = Kinvey.Files.upload(content, metadata, {
public: true
})
.then(function(file) {
console.log('File uploaded');
})
.catch(function(error) {
console.log('Upload error: ' + error.message);
});
}, function (error) {
console.log('fs read error: ' + error);
});
}).catch(function (err) {
console.log("Error -> " + err.message);
});

Reading file as string is the only way it is able to upload.


While uploading I'm getting error, but file is displayed in Kinvey console with its size. When I click this file in console as well as try to get this file with code I'm getting error:


<Error>

 

<Code>AccessDenied</Code>

<Message>Access denied.</Message>

<Details>

Anonymous users does not have storage.objects.get access to object 6d70d80512b04924a8157d2845123a81/06ceab9e-457b-4964-8bb3-40ab8e5ba14a/12222.jpg.

</Details>

</Error>


Can you help me and explain what I'm doing wrong?


Martin,


HelloWorld.zip works fine with uploading files and it looks like problem is on my side somewhere else. 


Thank you for your help!

Hi Pavel,


I am sorry to hear the problem persists. 

I am attaching a very simple project for you to try and test image uploading. Please change the following 4 variables to test the project on your side in main-page.js (appKey, appSecret, username, password):

 

    Kinvey.init({
        appKey: 'your-app-key-here',
        appSecret: 'your-app-sercret-here'
    });

    const activeUser = Kinvey.User.getActiveUser();
    if (!activeUser) {
        Kinvey.User.login('your-username', 'your-password');
    }

 

Furthermore, I would like to add some additional explanation of the File upload process - the Uploading to Kinvey is a two-step process as noted in the documentation. The process consists of:

  1. POST to Kinvey to create the file metadata and fetch the upload URL.
  2. Upload the file to Google Cloud Storage.
In the case you describe, the first part (post to Kinvey file metadata) is created, but the upload to GCS fails. In that case, when trying to open the file by its downloadURL stored in Kinvey and the file is public, GCS would return "Access denied" error.

Please note that the upload to GCS will take more time than the creation of the file metadata in Kinvey, so there could be a few seconds where the file metadata in Kinvey would exist but the file in GCS would not still be uploaded.

Try and test with the attached project and let me know the result. Also, try using a different network to test with if it fails again to rule out network related problems.

Regards,
Martin


zip
(2.13 MB)

1 person likes this

Hi Martin!


I've updated Kinvey to 3.7.2 and changed code as you provided in your post. Looks like it doesn't fix the issue.

The code in upload Promise (then&catch) is not executed, I don't see any console.log messages from then or catch blocks. But file appears in Kinvey console. Anyway it is not available for downloading both through console and code.


The error message is the same:


<?xml version='1.0' encoding='UTF-8'?>

<Error>

<Code>AccessDenied</Code>

<Message>Access denied.</Message>

<Details>Anonymous users does not have storage.objects.get access to object f15cfab1f6fc4735baddb9aa0d56c64a/d58fdd4d-4fc0-40d1-b611-d8c0f1c781fb/NSIMG_20170803_82145.jpg.</Details>

</Error>

NOTE: BACK-2726 (current), WEBCLI-2180 (renamed)

Hi Pavel,


The issue you are describing indicates that the metadata for the file is created in Kinvey but the actual file is not successfully uploaded to Google Cloud Storage.

Please try the following:

  1. Update Kinvey NativeScript SDK to the latest version (3.7.2) as it fixes file upload bugs.
  2. Try using the following code (where you are passing to the upload() function not the actual file content but a file object):


 

    camera.requestPermissions();
    camera.takePicture()
        .then(function (imageAsset) {
            console.log("Result is an image asset instance");
            console.dir(imageAsset);
            
            console.log('Uploading image');

            var filePath = imageAsset.android;
            console.log('exists: ' + fs.File.exists(filePath));

            var imageFile = fs.File.fromPath(filePath);
            console.dir(imageFile);
            var fileContent = imageFile.readSync();

            var metadataTrue = {
                filename: imageFile.name,
                mimeType: 'image/jpeg',
                size: fileContent.length,
                public: true
            }
           
            Kinvey.Files.upload(imageFile, metadataTrue)
                .then(function (file) {
                    console.log('File uploaded');
                    console.log(JSON.stringify(file));
                })
                .catch(function (error) {
                    console.log('Upload error: ' + error.message);
                });
        }).catch(function (err) {
            console.log("Error -> " + err.message);
        });

 

Let me know if this has fixed the issue or if the problem persists.


Regards,

Martin


1 person likes this

NOTE: BACK-2726

Hi Pavel,

The engineering team is currently working on the problem. We do not have to ETA yet but we will let you know as soon as we have an update for you.

Regards,
Abhijeet

Thank you for your answer.


Can you tell me when update with this issue fix will be released?

Pavel,

This is known issue and this has been escalated to engineering. Will keep you posted on the progress.

Thanks,
Pranav
WEBCLI-2180

 

Login or Signup to post a comment