定制 devhammed/byteship-php 二次开发

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

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

devhammed/byteship-php

Composer 安装命令:

composer require devhammed/byteship-php

包简介

PHP client for the Byteship Upload API.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads Laravel Compatibility

Table of Contents

Introduction

PHP client & Laravel storage adapter for Byteship.

Installation

You can install the package via composer:

composer require devhammed/byteship-php

Usage

The first thing you need to do is to get an API key at Byteship. You'll find more info at the Byteship Docs.

Create a Client

Create the client with a full project API key only on trusted server code. Browser code should use a short-lived upload token minted by your backend.

Server client

use Devhammed\Byteship\Client;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

Upload client

use Devhammed\Byteship\Client;

$byteship = new Client(uploadToken: 'bsut_...');

Keep API keys server-side!

Never ship a bship_... project API key to the browser. Use createUploadToken on your server and pass the returned bsut_... token to frontend code.

Upload a File

Use upload when you want the SDK to create the upload session, send the bytes to storage, complete the upload, and return the ready file.

use Devhammed\Byteship\Client;
use Devhammed\Byteship\Enums\Visibility;
use Devhammed\Byteship\ValueObjects\UploadProgress;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

$file = fopen('photo.jpg', 'rb');

$uploaded = $byteship->upload(
    $file,
    path: 'uploads/photo.jpg',
    visibility: Visibility::Public,
    metadata: [
        'user_id' => '123',
    ],
    onProgress: function (UploadProgress $progress) {
        echo round($progress->percent).'% uploaded';
    },
);

echo "#{$uploaded->id} - {$uploaded->url}";

Multiple Files

Use uploadMany for batches. Each result keeps the original file, a status, and either the uploaded file or a Byteship\Error.

use Devhammed\Byteship\Client;
use Devhammed\Byteship\Enums\Visibility;
use Devhammed\Byteship\Enums\UploadManyResultStatus;
use Devhammed\Byteship\ValueObjects\UploadManyProgress;
use Devhammed\Byteship\ValueObjects\UploadInput;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

$files = [
    new UploadInput(fopen('photo-1.jpg', 'rb')),
    new UploadInput(fopen('photo-2.jpg', 'rb')),
    new UploadInput(fopen('photo-3.jpg', 'rb')),
    new UploadInput(fopen('photo-4.jpg', 'rb')),
    new UploadInput(fopen('photo-5.jpg', 'rb')),
    new UploadInput(fopen('photo-6.jpg', 'rb')),
];

$results = $byteship->uploadMany(
    $files,
    concurrency: 3,
    pathPrefix: 'gallery',
    visibility: Visibility::Public,
    metadata: [
        'user_id' => '123',
    ],
    onFileProgress: function (UploadManyProgress $progress) {
        echo '#'.$progress->index.': '.round($progress->percent).'% uploaded';
    },
);

$uploaded = array_map(
    fn($item) => $item->result,
    array_filter($results, fn($item) => $item->status === UploadManyResultStatus::Fulfilled),
);

File Methods

Server clients can read file metadata, create temporary URLs for private files, download file, and delete stored files when the credential has the required scope.

use Devhammed\Byteship\Client;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

$fileResponse = $byteship->getFile('uploads/photo.jpg');

echo 'File Status: '.$fileResponse->file->status;

$signedResponse = $byteship->createSignedUrl(
    $fileResponse->file->path,
    expiresInSeconds: 10 * 60,
);

echo 'Signed URL: '.$signedResponse->signedUrl->url;

$stream = $byteship->downloadFile($fileResponse->file->path);

file_put_contents('photo.jpg', $stream);

echo 'Download Size: '.filesize('photo.jpg');

$deleted = $byteship->deleteFile($fileResponse->file->path);

echo 'File Status: '.$deleted->file->status;

Manual Flow

Use the lower-level methods when you need to own one part of the flow, such as sending the file bytes with a custom upload client.

use Devhammed\Byteship\Client;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\MimeType;
use GuzzleHttp\Psr7\Utils;
use RuntimeException;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

$filePath = 'manual/invoice.pdf';
$stream = Utils::streamFor(fopen($filePath, 'rb'));
$byteSize = $stream->getSize() ?? filesize($filePath);
$contentType = MimeType::fromFilename($filePath) ?? 'application/octet-stream';

$created = $byteship->createFileUpload(
    path: $filePath,
    contentType: $contentType,
    byteSize: $byteSize,
);

if (empty($created->upload->url)) {
    throw new RuntimeException('Upload URL missing');
}

$http = new GuzzleClient();

$http->request('PUT', $created->upload->url, [
    'headers' => $created->upload->headers,
    'body' => $stream,
]);

$completed = $byteship->completePathUpload(
    path: $created->file->path,
    uploadId: $created->upload->id,
);

echo $completed->file->status;

Errors

Every SDK API request throws Byteship\Error for non-2xx responses.

use Devhammed\Byteship\Client;
use Devhammed\Byteship\Error;

$byteship = new Client(apiKey: $_ENV['BYTESHIP_API_KEY']);

try {
    $byteship->createUploadToken(
        folder: 'uploads',
        maxUploadBytes: 10 * 1024 * 1024,
    );
} catch (Error $error) {
    echo "Error creating upload token: {$error->getError()} - {$error->getStatus()} - {$error->getMessage()}";
}

Laravel

This package ships with a service provider for Laravel that will automatically setup the client for your application.

To get started, create an environment variable named BYTESHIP_API_KEY in your .env file with your Byteship API key:

BYTESHIP_API_KEY=your-api-key

Then, open config/filesystems.php and add the byteship disk configuration:

return  [
    // ...

    'disks' => [
        'byteship' => [
            'driver' => 'byteship',
            'visibility' => 'public',
            'api_key' => env('BYTESHIP_API_KEY'),
        ],
    ],

    // ...
];

You should now be able to use the Byteship disk in your Laravel application just like any other storage drivers:

use Illuminate\Support\Facades\Storage;

Storage::disk('byteship')->put('hello.txt', 'Hello, Byteship!'); // true/false
Storage::disk('byteship')->get('hello.txt'); // "Hello, Byteship!"
Storage::disk('byteship')->url('hello.txt'); // "https://cdn.byteship.dev/f/12345/hello.txt" (only for public files)
Storage::disk('byteship')->temporaryUrl('hello.txt', now()->addHour()); // "https://cdn.byteship.dev/f/12345/hello.txt?token=secret-token" (only for private files)
Storage::disk('byteship')->temporaryUploadUrl('hello.txt', now()->addHour(), ['byte_size' => 1024]) // ['file_id' => '123', 'upload_id' => '456', 'upload_token' => 'd34db33f', 'url' => 'https://...', 'complete_url' => 'https://...', 'headers' => ['content-type' => 'text/plain']] (use the complete URL + upload ID + upload_token to complete the upload after sending the file to the url + headers)
Storage::disk('byteship')->delete('hello.txt'); // true/false
Storage::disk('byteship')->exists('hello.txt'); // true/false
Storage::disk('byteship')->mimeType('hello.txt'); // "text/plain"
Storage::disk('byteship')->visibility('hello.txt'); // "public" / "private"
Storage::disk('byteship')->size('hello.txt'); // 16

RECOMMENDED: You should set the default disk to byteship inside config/filesystems.php so you won't have to specify the disk name everytime you work with Storage.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

devhammed/byteship-php 适用场景与选型建议

devhammed/byteship-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 0 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 05 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「laravel」 「Hammed Oyedele」 「byteship-php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

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

围绕 devhammed/byteship-php 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-05-07