@@ -535,10 +535,16 @@ private function exportBuckets(int $batchsize)
535535 break ;
536536 }
537537
538+ $ buckets = [];
538539 foreach ($ result ['items ' ] as $ bucket ) {
539- $ this ->callback ([new Bucket ($ bucket ['id ' ], $ bucket ['name ' ], [], false )]);
540+ $ curBucket = new Bucket ($ this ->sanitizeBucketId ($ bucket ['id ' ]), $ bucket ['name ' ], [], false );
541+ $ curBucket ->setOriginalId ($ bucket ['id ' ]);
542+
543+ $ buckets [] = $ curBucket ;
540544 }
541545
546+ $ this ->callback ($ buckets );
547+
542548 if (! isset ($ result ['nextPageToken ' ])) {
543549 break ;
544550 }
@@ -547,13 +553,38 @@ private function exportBuckets(int $batchsize)
547553 }
548554 }
549555
556+ private function sanitizeBucketId ($ id )
557+ {
558+ // Step 1: Check if the ID looks like a URL (contains ".")
559+ if (strpos ($ id , '. ' ) !== false ) {
560+ // If it looks like a URL, try to extract the subdomain
561+ $ parts = explode ('. ' , $ id );
562+ if (count ($ parts ) > 0 ) {
563+ $ id = $ parts [0 ];
564+ }
565+ }
566+
567+ // Step 2: Ensure the ID contains at most 36 characters
568+ $ id = substr ($ id , 0 , 36 );
569+
570+ // Step 3: Remove invalid characters using a regular expression
571+ $ id = preg_replace ('/[^a-zA-Z0-9\._-]/ ' , '' , $ id );
572+
573+ // Step 4: Ensure the ID doesn't start with a special character
574+ if (preg_match ('/^[._-]/ ' , $ id )) {
575+ $ id = 'a ' .substr ($ id , 1 );
576+ }
577+
578+ return $ id ;
579+ }
580+
550581 private function exportFiles (int $ batchsize )
551582 {
552583 $ buckets = $ this ->cache ->get (Bucket::getName ());
553584
554585 foreach ($ buckets as $ bucket ) {
555586 /** @var Bucket $bucket */
556- $ endpoint = 'https://storage.googleapis.com/storage/v1/b/ ' .$ bucket ->getId ().'/o ' ;
587+ $ endpoint = 'https://storage.googleapis.com/storage/v1/b/ ' .$ bucket ->getOriginalId ().'/o ' ;
557588
558589 $ nextPageToken = null ;
559590
@@ -588,7 +619,7 @@ private function exportFiles(int $batchsize)
588619
589620 private function exportFile (File $ file )
590621 {
591- $ endpoint = 'https://storage.googleapis.com/storage/v1/b/ ' .$ file ->getBucket ()->getId ().'/o/ ' .$ file ->getId ().'?alt=media ' ;
622+ $ endpoint = 'https://storage.googleapis.com/storage/v1/b/ ' .$ file ->getBucket ()->getOriginalId ().'/o/ ' .$ file ->getId ().'?alt=media ' ;
592623 $ start = 0 ;
593624 $ end = Transfer::STORAGE_MAX_CHUNK_SIZE - 1 ;
594625
0 commit comments