6

Is there any way to upload directly to backblaze from the client? Right now images upload from the client to php and then to backblaze, so a file is uploaded twice. Can I just send the file to backblaze from Javascript?

MasudM
  • 99
  • 2
  • 11
  • 1
    What is Backblaze? How does it work? Do they use some kind of verification process before uploading is possible? Please add more details about Backblaze api. Don't expect us to magicly know how 3rd party software works. – icecub May 31 '16 at 20:07
  • 1
    Could you not upload files via JSON and Backblaze API: https://www.backblaze.com/b2/docs/calling.html ? Looks like the API is available only via B2 Cloud Storage so you might have to pay for the API calls. – Maximus2012 May 31 '16 at 20:10
  • @Maximus2012 I must of missed these docs! Thanks, I will check it out – MasudM May 31 '16 at 20:19
  • 1
    @MasudM have you found the solution ? can you share with us what you found – medBouzid Oct 09 '16 at 15:23
  • 1
    @medBo unfortunately, I could not find a solution. I tried contacting support and they said it is not possible yet. In my case, i switched to AWS S3 which allows Cross Origin Resource Sharing (aka CORS). – MasudM Oct 11 '16 at 18:53
  • @MasudM ok thanks man – medBouzid Oct 13 '16 at 15:16
  • 1
    @MasudM. It is now possible, I think: https://www.backblaze.com/b2/docs/cors_rules.html – Juan Sánchez Mar 26 '18 at 06:23

2 Answers2

7

I think it is possible to upload directly as CORS is now available for b2. Post file directly with ajax to upload file.

Step 1. Get authorizationToken token using account id and application key

step 2. Get upload url using above authorizationToken.

Step 3. Send this url to client browser and upload file directly to b2.

Problem: if you need sha1 for verification that is possible only for HTML5 supported browser. Read more here

Is it possible to compute a file's SHA1 ID using Javascript?

https://developer.mozilla.org/en-US/docs/Web/API/File

https://www.backblaze.com/b2/docs/b2_upload_file.html

Vishvendra Singh
  • 484
  • 5
  • 19
2

For anyone still looking for a solution, you can now use S3 pre-signed URL. Here are the steps

1- From your front end/JS send a GET/POST request to server (in this case PHP) requesting a pre-signed URL

$b2 = new Aws\S3\S3Client([
        'version' => 'latest',
        'endpoint' => 'https://xxx.backblazeb2.com',
        'region' => 'eu-xxx-xxx',
        'credentials' => array(
            'key'    => $_ENV['B2_KEY'],
            'secret' => $_ENV['B2_SECRKET'],
        )
    ]);

$cmd = $b2->getCommand('PutObject', [
            'Bucket' => $bucketName,
            'Key' => $subFolder . "" . $fileName,
            'ContentType' => $fileType,
        ]);
    
    
        $request = $b2->createPresignedRequest($cmd, '+20 minutes');
    
        // Get the actual presigned-url
        $presignedUrl = (string)$request->getUri();
    
        echo $presignedUrl;
    

2- Fetch your response in your front, and then upload your file using the presigned URL code (example with Axios)

axios
     .put(b2PreSignedUrl, myFile)
     .then(response => {
     console.log(" -> B2 upload status " + JSON.stringify(response))

      }).catch(e => {
        console.log(" -> Error uploading video to B2 " + e)
       })

BUT MOST IMPORTANTLY (or else you will get CORS errors or timeout error), you need to create a custom CORS for the bucket via the command line tool. Here is my CORS setup and CLI command

b2 update-bucket --corsRules '[    
          {
              "corsRuleName": "downloadFromAnyOriginWithUpload",
              "allowedOrigins": [
                  "*"
              ],
              "allowedOperations": [
                  "s3_delete",
                  "s3_get",
                  "s3_head",
                  "s3_post",
                  "s3_put"
              ],
              "maxAgeSeconds": 3600
          }
      ]' <BUCKETNAME> allPublic
Dharman
  • 30,962
  • 25
  • 85
  • 135
el_even
  • 654
  • 9
  • 10