Skip to content

Commit aa0bc33

Browse files
author
Bertus Steenberg
authored
Merge pull request Superbalist#66 from Superbalist/fix-custom-uri-bucket-prepend
FIX: No bucket name prepending when using a custom URI
2 parents 377f6fe + ff453cb commit aa0bc33

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,33 @@ $filesystem->deleteDir('path/to/directory');
9292

9393
// see http://flysystem.thephpleague.com/api/ for full list of available functionality
9494
```
95+
96+
## Google Storage specifics
97+
98+
When using a custom storage uri the bucket name will not prepended to the file path.
99+
100+
```php
101+
$storageClient = new StorageClient([
102+
'projectId' => 'your-project-id',
103+
]);
104+
$bucket = $storageClient->bucket('your-bucket-name');
105+
$adapter = new GoogleStorageAdapter($storageClient, $bucket);
106+
107+
// uri defaults to "https://storage.googleapis.com"
108+
$filesystem = new Filesystem($adapter);
109+
$filesystem->getUrl('path/to/file.txt');
110+
// "https://storage.googleapis.com/your-bucket-name/path/to/file.txt"
111+
112+
// set custom storage uri
113+
$adapter->setStorageApiUri('http://example.com');
114+
$filesystem = new Filesystem($adapter);
115+
$filesystem->getUrl('path/to/file.txt');
116+
// "http://example.com/path/to/file.txt"
117+
118+
// You can also prefix the file path if needed.
119+
$adapter->setStorageApiUri('http://example.com');
120+
$adapter->setPathPrefix('extra-folder/another-folder/');
121+
$filesystem = new Filesystem($adapter);
122+
$filesystem->getUrl('path/to/file.txt');
123+
// "http://example.com/extra-folder/another-folder/path/to/file.txt"
124+
```

src/GoogleStorageAdapter.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515

1616
class GoogleStorageAdapter extends AbstractAdapter
1717
{
18+
/**
19+
* @const STORAGE_API_URI_DEFAULT
20+
*/
21+
const STORAGE_API_URI_DEFAULT = 'https://storage.googleapis.com';
22+
1823
/**
1924
* @var StorageClient
2025
*/
@@ -28,7 +33,7 @@ class GoogleStorageAdapter extends AbstractAdapter
2833
/**
2934
* @var string
3035
*/
31-
protected $storageApiUri = 'https://storage.googleapis.com';
36+
protected $storageApiUri;
3237

3338
/**
3439
* @param StorageClient $storageClient
@@ -45,9 +50,7 @@ public function __construct(StorageClient $storageClient, Bucket $bucket, $pathP
4550
$this->setPathPrefix($pathPrefix);
4651
}
4752

48-
if ($storageApiUri) {
49-
$this->storageApiUri = $storageApiUri;
50-
}
53+
$this->storageApiUri = ($storageApiUri) ?: self::STORAGE_API_URI_DEFAULT;
5154
}
5255

5356
/**
@@ -83,8 +86,6 @@ public function setStorageApiUri($uri)
8386
/**
8487
* Return the storage api uri.
8588
*
86-
* @param string $uri
87-
*
8889
* @return string
8990
*/
9091
public function getStorageApiUri()
@@ -398,7 +399,15 @@ public function getUrl($path)
398399
{
399400
$uri = rtrim($this->storageApiUri, '/');
400401
$path = $this->applyPathPrefix($path);
401-
return $uri . '/' . $this->bucket->name() . '/' . $path;
402+
403+
// Only prepend bucket name if no custom storage uri specified
404+
// Default: "https://storage.googleapis.com/{my_bucket}/{path_prefix}"
405+
// Custom: "https://example.com/{path_prefix}"
406+
if ($this->getStorageApiUri() === self::STORAGE_API_URI_DEFAULT) {
407+
$path = $this->bucket->name() . '/' . $path;
408+
}
409+
410+
return $uri . '/' . $path;
402411
}
403412

404413
/**

tests/GoogleStorageAdapterTests.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,7 @@ public function testGetUrl()
918918

919919
$adapter->setStorageApiUri('http://my-domain.com/');
920920
$adapter->setPathPrefix('another-prefix');
921-
$this->assertEquals('http://my-domain.com/my-bucket/another-prefix/dir/file.txt', $adapter->getUrl('dir/file.txt'));
921+
// no bucket name on custom domain
922+
$this->assertEquals('http://my-domain.com/another-prefix/dir/file.txt', $adapter->getUrl('dir/file.txt'));
922923
}
923924
}

0 commit comments

Comments
 (0)