2017-09-21 162 views
0

我正在使用PHP的Google雲端存儲SDK來上傳資源,但我有問題使我上傳的文件公開可用,例如我在上傳配置中嘗試使用acl =>['READER' => 'allUsers']Google Cloud Storage API - 新ACL?

無論如何,我希望能夠分享我上傳到allUsers用戶的所有資源,因此可以公開訪問這些圖片。

這裏是我的代碼:

public function upload($sourceFilename, $destFilename) { 

     $bucket = $this->storage->bucket(self::IMAGE_BUCKET_NAME); 

     //Base options 
     $options = [ 
      'name' => $destFilename, 
      'validate' => true, 
      'acl' => [ 
       'READER' => 'allUsers' 
      ] 
     ]; 

     //Local file read: 
     $fileHandle = @fopen($sourceFilename, 'r'); 

     if ($fileHandle === false) { 
      throw new \Exception("Failed to open file for upload."); 
     } 

     //Resumable uploader: 
     $uploader = $bucket->getResumableUploader($fileHandle, $options); 


     try { 
      $object = $uploader->upload(); 

     } catch (GoogleException $ex) { 
      $resumeUri = $uploader->getResumeUri(); 
      $object = $uploader->resume($resumeUri); 
      //do some error handling ? 
     } 
    } 

回答

1

嘗試改變 'ACL' 部分:

'predefinedAcl' => 'publicRead' 
+0

謝謝!這工作!將在5分鐘內接受答案:) –

相關問題