定制 steppinghat/backblaze-b2-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

steppinghat/backblaze-b2-bundle

Composer 安装命令:

composer require steppinghat/backblaze-b2-bundle

包简介

A Symfony compatible bundle for interfacing with the Backblaze B2 API

README 文档

README

Latest Stable Version Total Downloads License PHP Version Require

📼 Backblaze B2 API client services for Symfony projects

Installation

To install this bundle, run the following in your Symfony project directory:

$ composer require steppinghat/backblaze-b2-bundle

Then register the bundle in config/bundles.php

<?php

return [
    // ...
    SteppingHat\BackblazeB2\BackblazeB2Bundle::class => ['all' => true]
]

And lastly, create a config file in config/packages called backblaze_b2.yaml

backblaze_b2:
  account_id: '%env(BACKBLAZE_MASTER_ACCOUNT_ID)%'
  application_id: '%env(BACKBLAZE_MASTER_APPLICATION_ID)%'
  application_secret: '%env(BACKBLAZE_MASTER_APPLICATION_KEY)%'
  token_cache_directory: ~

Configuration

Backblaze API keys

Before setting off, you'll need to create a key by going to App Keys in your Backblaze Account.

Not all keys are created equal!

You can use an application key, however the capabilities are very limited in comparison to the master application key. Please bare this in mind when configuring the bundle.

Caching Tokens

In order to both reduce the number of API calls made, and speed up the overall turnaround time, we can optionally cache tokens to disk. This means that instead of requesting the authentication token needed to carry out requests every time, we use one from an earlier call that we've cached. These tokens last ~12 hours before they expire and a new one is needed.

While is can be preferable, some might not like the idea of keeping a token saved to disk for security reasons. If your system is well secured and this isn't as much of a risk, turn this on by setting tokenCacheDirectory to a valid directory in that we have permissions to write to.

This cache is not cleared by running php bin/console cache:clear

Usage

For all basic usage of the API, simply use the BackblazeClient class in your service.

<?php

use SteppingHat\BackblazeB2\Client\BackblazeClient;

class ExampleService {

    protected BackblazeClient $client;

    public function __construct(BackblazeClient $client) {
        $this->client = $client;    
    }
    
}

Create a bucket

Requires the writeBuckets capability

$bucket = $client->createBucket('bucketName', Bucket::TYPE_PRIVATE);

List all buckets

Requires the listBuckets capability

$buckets = $client->listBuckets();

Update a bucket

Requires the writeBuckets capability

$bucket = $client->updateBucket($bucket, Bucket::TYPE_PUBLIC);

Delete a bucket

Requires the deleteBuckets capability

$client->deleteBucket($bucket);

List all files

Requires the listFiles capability

// List all files across all buckets
$files = $client->listFiles();

// List all files in a specific bucket
$files = $client->listFiles($bucket);

// Search for a specific file
$files = $client->listFiles($bucket, 'animals/dogs/floof.png');

// Search for all files matching a prefix
$files = $client->listFiles($bucket, null, 'animals/dogs/');

Check if a file exists

Requires the listFiles capability

if($client->fileExists($bucket, 'animals/dogs/doggo.jpg')) {
    // ...
}

Get file info

Requires the readFiles capability

$file = $client->getFileInfo($file);
// or
$file = $client->getFileInfoById($fileId);

Delete a file

Requires the deleteFiles capability

$client->deleteFile($file);

If a file has governance restrictions, access keys with the bypassGovernance capability can force delete files.

$client->deleteFile($file, true);

Upload a file

Requires the writeFiles capability

Files can be uploaded by either passing a string content

$fileContent = 'Hello world!';
// or
$fileContent = file_get_contents('hello.txt');
$client->upload($bucket, $fileContent, 'files/hello.txt');

or files can be passed in as a resource

$file = fopen('smoldoggo.png', 'r');
$client->upload($bucket, $file, 'animals/dogs/smoldoggo.png');

⚠ Resource content is loaded into memory before being sent. This is due to a limitation where the Symfony HttpClient library sends headers after the content, and the Backblaze API expects it before. As a result, we can't directly pass the resource directly to Backblaze. This is currently a //TODO to find a workaround for. If you intend to upload large files, this may be an issue.

Download a file

Requires the readFiles capability

Downloaded content is simply returned into a variable

$content = $client->download($file);
echo $file;
// Hello world!

but can also be passed directly into a resource

$resource = fwrite('/tmp/smoldoggo.png', 'w');
$client->download($file, $resource);
fclose($resource);

Testing

In terms of tests, there are no tests. (Boooo).

This library is still a work in progress as I don't yet fully support the B2 API, nor have I ironed out 100% of the kinks (see uploading a resource for example).

Most of the functionality works (I use it in my own app), I guess you can call that some sort of guarantee?

But don't worry, once I've built out the library some more there will be tests - I promise!

License

Made with ❤️ by Javan Eskander

Available for use under the MIT license.

steppinghat/backblaze-b2-bundle 适用场景与选型建议

steppinghat/backblaze-b2-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 320 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 06 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 steppinghat/backblaze-b2-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 steppinghat/backblaze-b2-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 steppinghat/backblaze-b2-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 320
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 16
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-12