public function upload(array $options)
    {
        // Clean the path if it starts with /.
        if (substr($options['FileName'], 0, 1) === '/') {
            $options['FileName'] = ltrim($options['FileName'], '/');
        }

        if (!isset($options['BucketId']) && isset($options['BucketName'])) {
            $options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
        }

        // Retrieve the URL that we should be uploading to.
        $response = $this->client->request('POST', $this->apiUrl.'/b2_get_upload_url', [
            'headers' => [
                'Authorization' => $this->authToken
            ],
            'json' => [
                'bucketId' => $options['BucketId']
            ]
        ]);

        $uploadEndpoint = $response['uploadUrl'];
        $uploadAuthToken = $response['authorizationToken'];

        if (is_resource($options['Body'])) {
            // We need to calculate the file's hash incrementally from the stream.
            $context = hash_init('sha1');
            hash_update_stream($context, $options['Body']);
            $hash = hash_final($context);

            // Similarly, we have to use fstat to get the size of the stream.
            $size = fstat($options['Body'])['size'];
            
            // Rewind the stream before passing it to the HTTP client.
            rewind($options['Body']);
        } else {
            // We've been given a simple string body, it's super simple to calculate the hash and size.
            $hash = sha1($options['Body']);
            $size = mb_strlen($options['Body']);
        }

        if (!isset($options['FileLastModified'])) {
            $options['FileLastModified'] = round(microtime(true) * 1000);
        }

        if (!isset($options['FileContentType'])) {
            $options['FileContentType'] = 'b2/x-auto';
        }

        $response = $this->client->request('POST', $uploadEndpoint, [
            'headers' => [
                'Authorization' => $uploadAuthToken,
                'Content-Type' => $options['FileContentType'],
                'Content-Length' => $size,
                'X-Bz-File-Name' => $options['FileName'],
                'X-Bz-Content-Sha1' => $hash,
                'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified']
            ],
            'body' => $options['Body']
        ]);

        return new File(
            $response['fileId'],
            $response['fileName'],
            $response['contentSha1'],
            $response['contentLength'],
            $response['contentType'],
            $response['fileInfo']
        );
    }
public function upload(array $options)
    {
        // Clean the path if it starts with /.
        if (substr($options['FileName'], 0, 1) === '/') {
            $options['FileName'] = ltrim($options['FileName'], '/');
        }

        if (!isset($options['BucketId']) && isset($options['BucketName'])) {
            $options['BucketId'] = $this->getBucketIdFromName($options['BucketName']);
        }

        // Retrieve the URL that we should be uploading to.
        $response = $this->client->request('POST', $this->apiUrl.'/b2_get_upload_url', [
            'headers' => [
                'Authorization' => $this->authToken
            ],
            'json' => [
                'bucketId' => $options['BucketId']
            ]
        ]);

        $uploadEndpoint = $response['uploadUrl'];
        $uploadAuthToken = $response['authorizationToken'];

        if (is_resource($options['Body'])) {
            // We need to calculate the file's hash incrementally from the stream.
            $context = hash_init('sha1');
            hash_update_stream($context, $options['Body']);
            $hash = hash_final($context);

            // Similarly, we have to use fstat to get the size of the stream.
            $size = fstat($options['Body'])['size'];
            
            // Rewind the stream before passing it to the HTTP client.
            rewind($options['Body']);
        } else {
            // We've been given a simple string body, it's super simple to calculate the hash and size.
            $hash = sha1($options['Body']);
            $size = mb_strlen($options['Body']);
        }

        if (!isset($options['FileLastModified'])) {
            $options['FileLastModified'] = round(microtime(true) * 1000);
        }

        if (!isset($options['FileContentType'])) {
            $options['FileContentType'] = 'b2/x-auto';
        }

        $response = $this->client->request('POST', $uploadEndpoint, [
            'headers' => [
                'Authorization' => $uploadAuthToken,
                'Content-Type' => $options['FileContentType'],
                'Content-Length' => $size,
                'X-Bz-File-Name' => $options['FileName'],
                'X-Bz-Content-Sha1' => $hash,
                'X-Bz-Info-src_last_modified_millis' => $options['FileLastModified']
            ],
            'body' => $options['Body']
        ]);

        return new File(
            $response['fileId'],
            $response['fileName'],
            $response['contentSha1'],
            $response['contentLength'],
            $response['contentType'],
            $response['fileInfo']
        );
    }
public function upload ( array $ options ) { // Clean the path if it starts with /. if ( substr ( $ options [ 'FileName' ] , 0 , 1 ) === '/' ) { $ options [ 'FileName' ] = ltrim ( $ options [ 'FileName' ] , '/' ) ; } if ( ! isset ( $ options [ 'BucketId' ] ) && isset ( $ options [ 'BucketName' ] ) ) { $ options [ 'BucketId' ] = $ this -> getBucketIdFromName ( $ options [ 'BucketName' ] ) ; } // Retrieve the URL that we should be uploading to. $ response = $ this -> client -> request ( 'POST' , $ this -> apiUrl . '/b2_get_upload_url' , [ 'headers' => [ 'Authorization' => $ this -> authToken ] , 'json' => [ 'bucketId' => $ options [ 'BucketId' ] ] ] ) ; $ uploadEndpoint = $ response [ 'uploadUrl' ] ; $ uploadAuthToken = $ response [ 'authorizationToken' ] ; if ( is_resource ( $ options [ 'Body' ] ) ) { // We need to calculate the file's hash incrementally from the stream. $ context = hash_init ( 'sha1' ) ; hash_update_stream ( $ context , $ options [ 'Body' ] ) ; $ hash = hash_final ( $ context ) ; // Similarly, we have to use fstat to get the size of the stream. $ size = fstat ( $ options [ 'Body' ] ) [ 'size' ] ; // Rewind the stream before passing it to the HTTP client. rewind ( $ options [ 'Body' ] ) ; } else { // We've been given a simple string body, it's super simple to calculate the hash and size. $ hash = sha1 ( $ options [ 'Body' ] ) ; $ size = mb_strlen ( $ options [ 'Body' ] ) ; } if ( ! isset ( $ options [ 'FileLastModified' ] ) ) { $ options [ 'FileLastModified' ] = round ( microtime ( true ) * 1000 ) ; } if ( ! isset ( $ options [ 'FileContentType' ] ) ) { $ options [ 'FileContentType' ] = 'b2/x-auto' ; } $ response = $ this -> client -> request ( 'POST' , $ uploadEndpoint , [ 'headers' => [ 'Authorization' => $ uploadAuthToken , 'Content-Type' => $ options [ 'FileContentType' ] , 'Content-Length' => $ size , 'X-Bz-File-Name' => $ options [ 'FileName' ] , 'X-Bz-Content-Sha1' => $ hash , 'X-Bz-Info-src_last_modified_millis' => $ options [ 'FileLastModified' ] ] , 'body' => $ options [ 'Body' ] ] ) ; return new File ( $ response [ 'fileId' ] , $ response [ 'fileName' ] , $ response [ 'contentSha1' ] , $ response [ 'contentLength' ] , $ response [ 'contentType' ] , $ response [ 'fileInfo' ] ) ; }
Uploads a file to a bucket and returns a File object.

@param array $options
@return File
Uploads a file to a bucket and returns a File object .