如何读取 Google Cloud Storage 上的私有文件

德米特里亚达姆

我想创建一个媒体存储应用程序,允许用户在我的平台上上传和查看他们的媒体。我使用 laravel 来创建它。我已成功将其上传到具有私有文件属性的谷歌云存储,并将文件名保存到我的数据库中。

我的问题是,如何直接从 laravel 访问它,并将其显示为图像或视频,目前我有 cide.json 文件作为密钥,但我从未得到如何使用它的详细说明。

亚里士多德11

好的,如果我想象你的表结构,它可能像

id   user_id   file_location

你可以构建一个像

Route::get('users/{id}/files', FolderName\ControllerName@ControllerFunction)

现在您可以通过传递 id 来获取您的用户媒体文件,或者您可以根据您的想法修改您的控制器以获取所有用户媒体文件。

简要说明:

Routes.php在 Laravel 项目的文件中添加此路由

Route::get('users/{id}/files', User\UserApiController@fetchUserMediaFiles);

创建一个新的控制器来管理对服务功能的请求的流程

class UserApiController {

   public $userService;

   public function __construct(UserService $userService) {

        $this->userService = $userService;

   }

   public function fetchUserMediaFiles($id) {
     
         $userMediaFiles = $this->userService->fetchUserMediaFiles($id);

         return $this->done(null, [
             'userMediafiles' => $userMediaFiles
         ]); 

   }

}

创建一个新服务来处理来自 Controller 的传入请求并将其发送到存储库

class UserService {

   private $userRepository;

   public function __construct(UserRepository $userRepository) {

       $this->userRepository= $userRepository;

   }

   public function fetchUserMediaFiles($id) {
    
      return $this->userRepository->fetchUserMediaFiles($id);

   }
}

创建新的存储库,它将接受来自 userService 类的请求,并通过从您的数据库中获取数据来返回您的数据

class UserRepository {

public function fetchUserMediaFiles($id) {

    $userMediaFilesTable = UserMediaFiles::$TABLE_NAME;

    $model  = UserMediaFiles::with([])
                ->where("$userMediaFilesTable.user_id", $id);
    
    $model = $model->select(["$userMediaFilesTable.user_id", "$userMediaFilesTable.file_location"])
                    ->get();

    return $model;

    }
}

现在让我们尝试使用一个虚拟的 get 请求来访问 api

http://xxxxxxxx/api/users/1/files

你的反应是这样的

{
"userMediafiles": [
    {
        "user_id": 1,
        "file_location": "https://xxxxx.com/file",
    }
   
]

}

这就是你在 Laravel 中所做的。希望这个解释有帮助。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何从Google App Engine读取Google Cloud Storage文件

如何从Google Cloud Storage解密文件?

TensorBoard无法读取Google Cloud Storage上的摘要

努力从 Google Cloud Storage 存储桶中读取 csv 文件

如何使用带有 Python 的 Google Cloud Functions 将列表写入 Google Cloud Storage 中的文件

GCP Cloud Function从Cloud Storage读取文件

如何使用Spark-Scala本地程序从Google Cloud Storage中读取简单文本文件

如何使用curl从Google Cloud Storage下载文件

如何让 Google Cloud Storage 解压缩 gzip 文件?

如何限制Google Cloud Storage存储桶的文件类型?

如何根据Blob键从Google Cloud Storage获取文件的类型?

如何使用Deployment Manager将文件写入Google Cloud Storage?

如何批量删除 Google Cloud Storage 中的文件?(节点.js)

如何使用 google-cloud-storage 创建文件?

Cloud ML无法在Google Cloud Storage上找到文件

如何通过 Pandas 从 Google Cloud Function 中的 Google Cloud Storage 访问 csv 文件?

如何使用Google Cloud Dataflow将压缩文件写入Google Cloud Storage?

如何在google-cloud-ml作业或Google Cloud Storage中加载numpy npz文件?

如何从 Google Cloud Storage CSV 读取并将其加载到 Google Data Store

如何在Google Cloud Storage中存储的文件上使用cv2.imread?

如何从Google Cloud Functions NodeJS连接到Google Cloud Storage

从 Google Cloud Storage 读取并加载到 Google Data Store

使用Google Cloud Dataflow合并Google Cloud Storage中的文件

如何提供Google Cloud Storage映像?

如何从python函数登录Google Cloud Storage?

如何获取Google Cloud Storage的授权令牌

如何使用python遍历Google Cloud Storage子目录中的所有文件名?

如何使用带有校验和控制的Java从Google Cloud Storage下载大文件

如何使用PHP从Google Cloud Storage的存储桶中列出所有文件?