admin管理员组

文章数量:1287668

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?

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?

Share Improve this question edited Aug 12, 2024 at 16:34 metadaddy 4,4192 gold badges24 silver badges51 bronze badges asked May 31, 2016 at 20:02 MasudMMasudM 1192 silver badges11 bronze badges 7
  • 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 Commented May 31, 2016 at 20:07
  • 1 Could you not upload files via JSON and Backblaze API: backblaze./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 Commented May 31, 2016 at 20:10
  • 1 @MasudM have you found the solution ? can you share with us what you found – medBouzid Commented Oct 9, 2016 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 Commented Oct 11, 2016 at 18:53
  • 1 @MasudM. It is now possible, I think: backblaze./b2/docs/cors_rules.html – Juan Sánchez Commented Mar 26, 2018 at 6:23
 |  Show 2 more ments

2 Answers 2

Reset to default 8

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 pute a file's SHA1 ID using Javascript?

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

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

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.',
        '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 mand line tool. Here is my CORS setup and CLI mand

b2 update-bucket --corsRules '[    
          {
              "corsRuleName": "downloadFromAnyOriginWithUpload",
              "allowedOrigins": [
                  "*"
              ],
              "allowedOperations": [
                  "s3_delete",
                  "s3_get",
                  "s3_head",
                  "s3_post",
                  "s3_put"
              ],
              "maxAgeSeconds": 3600
          }
      ]' <BUCKETNAME> allPublic

本文标签: javascriptUpload to backblaze from clientStack Overflow