Skip to content

Commit

Permalink
Use S3 compatible driver for interacting with GCS (#727)
Browse files Browse the repository at this point in the history
* feat: use s3 driver for storing files in gcp bucket

* refactor: improve naming

* refactor: rename disk

* docs: update changelog

* fix: specify file visibility for public assets
  • Loading branch information
m90 authored Jan 22, 2024
1 parent 09e7b44 commit 938c3c5
Show file tree
Hide file tree
Showing 10 changed files with 464 additions and 425 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
# api

## 8x.35.1 - 22 January 2024
- Use S3 driver for accessing static files in GCP buckets, using proper ACLs

## 8x.35.0 - 19 January 2024
- Use pessimistic DB locks during Query Service Batch transactions

## 8x.34.1 - 18 January 2024
- Add missing QuestyCaptcha settings to wiki details response
- Revert "Use S3 driver for accessing static files in GCP buckets"

## 8x.34.0 - 17 January 2024
## 8x.34.0 - 17 January
- Use S3 driver for accessing static files in GCP buckets

## 8x.33.0 - 03 January 2024
Expand Down
12 changes: 2 additions & 10 deletions app/Jobs/DeleteWikiFinalizeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@
use App\WikiManager;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Log;
use Illuminate\Contracts\Filesystem\Cloud;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use App\Helper\ElasticSearchHelper;
use App\Http\Curl\HttpRequest;
use Google\Cloud\Core\Exception\GoogleException;

class DeleteWikiFinalizeJob extends Job implements ShouldBeUnique
{
Expand Down Expand Up @@ -99,7 +97,7 @@ public function handle( HttpRequest $request )

public function deleteSiteDirectory( int $wiki_id ): bool {
try {
$disk = Storage::disk('gcs-public-static');
$disk = Storage::disk('static-assets');
if (! $disk instanceof Cloud) {
$this->fail(new \RuntimeException("Invalid storage (not cloud)."));
return false;
Expand All @@ -112,13 +110,7 @@ public function deleteSiteDirectory( int $wiki_id ): bool {
return true;
}

// TODO add support for local files on minikube cluster
// Probably involves breaking out 'gcs-public-static' into some config setting etc.
} catch ( GoogleException $ex ) {
if( !file_exists( '/var/run/secret/cloud.google.com/key.json' ) ) {
return true;
}

} catch ( \Exception $ex ) {
$this->fail($ex);
return false;

Expand Down
10 changes: 6 additions & 4 deletions app/Jobs/SetWikiLogo.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function handle(): void
$wiki = $wikis->first();

// Get the cloud disk we use to store logos
$storage = Storage::disk('gcs-public-static');
$storage = Storage::disk('static-assets');
if (!$storage instanceof FilesystemAdapter) {
# TODO: Use a more specific exception?
$this->fail(new \RuntimeException("Invalid storage (not cloud)"));
Expand All @@ -66,7 +66,7 @@ public function handle(): void
$logosDir = Wiki::getLogosDirectory($wiki->id);

// Upload the local image to the cloud storage
$storage->putFileAs($logosDir, new File($this->logoPath), "raw.png");
$storage->putFileAs($logosDir, new File($this->logoPath), "raw.png", ['visibilty' => 'public']);

// Store a conversion for the actual site logo
$reducedPath = $logosDir . '/135.png';
Expand All @@ -75,7 +75,8 @@ public function handle(): void
}
$storage->writeStream(
$reducedPath,
Image::make($this->logoPath)->resize(135, 135)->stream()->detach()
Image::make($this->logoPath)->resize(135, 135)->stream()->detach(),
['visibility' => 'public'],
);

// Store a conversion for the favicon
Expand All @@ -85,7 +86,8 @@ public function handle(): void
}
$storage->writeStream(
$faviconPath,
Image::make($this->logoPath)->resize(64, 64)->stream()->detach()
Image::make($this->logoPath)->resize(64, 64)->stream()->detach(),
['visibility' => 'public'],
);

// Get the urls
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
"laravel/tinker": "^2.6",
"laravel/ui": "^3.3",
"lcobucci/jwt": "^4.1",
"league/flysystem-aws-s3-v3": "^1.0",
"maclof/kubernetes-client": "^0.26.0",
"mxl/laravel-job": "^1.2",
"percymamedy/laravel-dev-booter": "^3.0",
"php-http/guzzle6-adapter": "^2.0",
"php-http/message": "^1.16",
"php-http/message-factory": "^1.1",
"predis/predis": "^1.1",
"superbalist/laravel-google-cloud-storage": "^2.2"
"predis/predis": "^1.1"
},
"require-dev": {
"barryvdh/laravel-ide-helper": ">=2.8.0 <2.9.2",
Expand Down
Loading

0 comments on commit 938c3c5

Please sign in to comment.