PHP code example of devhammed / byteship-php

1. Go to this page and download the library: Download devhammed/byteship-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devhammed / byteship-php example snippets


use Devhammed\Byteship\Client;

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

use Devhammed\Byteship\Client;

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

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}";

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),
);

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;

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;

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()}";
}

return  [
    // ...

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

    // ...
];

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
bash
composer