2

I have spent an awful lot of time trying to upload files to b2 using clientside ajax requests (vue-dropzone.js), and even though I supplied the file's valid sha1 checksum, the b2 server still responds with "checksum did not match data received" with status code 400. I've checked and rechecked the checksums with all the tools I have and I'm still not able to trace the source of the error. Its as if something happens to the file while its in transit or something.

I've uploaded the same files using the command line tool and it works fine but when I upload via ajax using the exact same sha1 checksum it doesn't work.

My questions are:

  1. Does b2 even allow file uploads through ajax?

  2. If it does allow uploads via ajax then what am i doing wrong?

  3. Does the files remain valid when uploaded using "X-Bz-Content-Sha1", " do_not_verify". Cause I've tried that only to get invalid files when I downloaded them back.

  4. Are there other things I need to know about uploading files to b2 using ajax requests

Please view my ajax codes see if I got anything wrong:

             sending(file, xhr, formData) {
                // This function runs for each file right before they are sent by dropezone.
                // This is a good opportunity to insert file specific values
                // in this case the file's upload url, name and auth token
                let fileName = '';
                console.log('this is file type', file.type);
                if (file.type.includes('image')) {
                    fileName = 'images/${uuid.v1()}.png';
                } else if (file.type.includes('video')) {
                    fileName = 'videos/${uuid.v1()}.${file.type.split(' / ')[1]}';
                }
    
                const url = appConfig.serverAddress + '/catalog/submitFiles';
                console.log('this is sha1_hash', this.uploadInfo.sha1_hash);
                // open the xhr request and insert the file's upload url here
                xhr.open('Post', this.uploadInfo.url, true);
    
                // set b2's mandatory request headers
                // xhr.setRequestHeader(
                //  'Authorization',
                //  'Bearer ' + store.getters.getUserIdToken,
                // );
                xhr.setRequestHeader('Authorization', this.uploadInfo.authorizationToken);
                xhr.setRequestHeader('X-Bz-Content-Sha1', this.uploadInfo.sha1_hash);
                xhr.setRequestHeader('X-Bz-File-Name', fileName);
                xhr.setRequestHeader('Content-Type', 'b2/x-auto');
    
                formData = new FormData();
                formData.append('files', file);
    
                // the rest will be handled by dropzones upload pipeline
            }
Gabriel Devillers
  • 3,155
  • 2
  • 30
  • 53

1 Answers1

0

You are sending the file using form encoding, which causes the SHA-1 validation to fail since B2 is expecting the raw file data, with no encoding. The doc for b2_upload_file says:

The file to be uploaded is the message body and is not encoded in any way. It is not URL encoded. It is not MIME encoded.

I'm not an expert on vue-dropzone, but I'm guessing you need to just delete the two lines referencing formData. It looks like the default upload pipeline will send the raw file content.

metadaddy
  • 4,234
  • 1
  • 22
  • 46