Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/boytur/backblaze-b2-laravel-10-native-api
How to upload image file to backblaze-b2 with native api in laravel10
https://github.com/boytur/backblaze-b2-laravel-10-native-api
b2 blackblaze blackblaze-b2 laravel10 native-api
Last synced: about 17 hours ago
JSON representation
How to upload image file to backblaze-b2 with native api in laravel10
- Host: GitHub
- URL: https://github.com/boytur/backblaze-b2-laravel-10-native-api
- Owner: boytur
- Created: 2024-02-26T03:38:59.000Z (11 months ago)
- Default Branch: master
- Last Pushed: 2024-02-26T16:15:41.000Z (11 months ago)
- Last Synced: 2024-10-12T11:40:15.889Z (3 months ago)
- Topics: b2, blackblaze, blackblaze-b2, laravel10, native-api
- Language: PHP
- Homepage:
- Size: 82 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# How to upload image file to backblaze-b2 with native api in laravel10
- Thank you for the concepts https://grantwinney.com/what-is-backblaze-b2-api
- Docs https://www.backblaze.com/apidocs/introduction-to-the-b2-native-api
### 1. Install Laravel project
```
composer create-project laravel/laravel example-appcd example-app
```### 2. Install guzzlehttp
```
composer require guzzlehttp/guzzle
```### 3. In yout ENV file
```
B2_ACCOUNT_ID =
B2_APPLICATION_KEY =
B2_BUCKET_NAME =
B2_BUCKET_ID =
B2_API = https://api.backblazeb2.com
```
### 4 Crate folder Commands in app/console### 5. Create command to refresh the token
```
php artisan make:command RefreshB2Token
```### 6. In your RefreshB2Token.php
```
env('B2_API'),
'headers' => [
'Authorization' => 'Basic ' . base64_encode(env('B2_ACCOUNT_ID') . ':' . env('B2_APPLICATION_KEY'))
]
]);try {
$response = $b2->get('/b2api/v1/b2_authorize_account');
if ($response->getStatusCode() === 200) {
$res = $response->getBody()->getContents();
$responseData = json_decode($res, true);
$authorizationToken = $responseData['authorizationToken'];
$apiUrl = $responseData['apiUrl'];// Put it to cache
Cache::put('authorizationToken', $authorizationToken);
Cache::put('apiUrl', $apiUrl);$this->uploadUrlAndAuthorization();
}} catch (\Exception $e) {
return 'Failed to connect to Backblaze B2 API: ' . $e->getMessage();
}$this->info('B2 Authorization token and Upload URL have been refreshed successfully.');
}// Get uploadUrl and uploadUrl authorization token using authorizationToken and apiUrl
public function uploadUrlAndAuthorization()
{
try {$apiUrl = env('apiUrl');
$b2 = new Client([
'base_uri' => $apiUrl,
'headers' => [
'Authorization' => Cache::get('authorizationToken')
]
]);$apiUrl = Cache::get('apiUrl');
$response = $b2->get($apiUrl .'/b2api/v3/b2_get_upload_url?bucketId='.env('B2_BUCKET_ID '));if ($response->getStatusCode() === 200) {
$res = $response->getBody()->getContents();
$responseData = json_decode($res, true);
$uploadUrl = $responseData['uploadUrl'];
$authorizationTokenUrl = $responseData['authorizationToken'];Cache::put('uploadUrl', $uploadUrl);
Cache::put('authorizationTokenUrl', $authorizationTokenUrl);
}} catch (\Exception $e) {
return 'Fail to get authorization token and apiUrl' . $e->getMessage();
}
}
}```
### 7. Create schedule for refresh your token in app\Console\Kernel.php```
command('b2:refresh-token')->hourly();
}/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__ . '/Commands');require base_path('routes/console.php');
}
}```
### 8. Create controller for testig
```
php artisan make:controller FileUploadController
```### 9. In your FileUploadController.php
```
hasFile('file')) {$file = $request->file('file');
$fileContent = file_get_contents($file->path());$client = new Client([
'base_uri' => $uploadUrl,
'headers' => [
'Authorization' => $uploadUrlAuthorizationToken,
'Content-Type' => 'application/octet-stream',
'X-Bz-File-Name' => $file->getClientOriginalName(),
'X-Bz-Content-Sha1' => sha1_file($file->path()),
],
'body' => $fileContent,
]);try {
$response = $client->post($uploadUrl);
if ($response->getStatusCode() === 200) {
return 'File ' . $file->getClientOriginalName() . ' uploaded successfully.';
} else {
return 'Failed to upload file ' . $file->getClientOriginalName();
}
} catch (\Exception $e) {
return 'Failed to upload file: ' . $e->getMessage();
}
} else {
return 'No files uploaded.';
}
}
}```
### 10. Edit your view welcome.blade.php
```
File Upload
File Upload
@csrf
Upload
```
### 11. Edit your route in web.php```
You can using dd($uploadUrl,$uploadUrlAuthorizationToken) in your FileUploadController.php for sure.```
php artisan b2:refresh-token
```### 13. Start your project and test upload image
```
php artisan serve
```#### In my backblaze-b2