Phanix
Phanix

Just writing

Mount GCP storage 给apache php 存取

遇到的情况是要在ubuntu 机器上mount 非public access 的gcp storage bucket,然后要给php web api 做读写。

比较正常的解决方式应该是用google cloud storage api for PHP( https://googleapis.github.io/google-cloud-php/#/docs/cloud-storage/v1.24.1/storage/storageclient 使用说明& https://github.com/googleapis/google-cloud-php-storage github ),但因为要赶着上线并把原本储存在local disk 改去gcp cloud storage去,所以赶着做就先用mount bucket 的方式去做读写。

先安装gcsfuse

安装说明可以直接看https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/docs/installing.md

mount with gcsfuse

可以看https://github.com/GoogleCloudPlatform/gcsfuse/blob/master/docs/mounting.md ,但是要注意的是单纯地做mount 则路径权限会跟着mount 的帐号。所以,假设用root mount,那么通常ubuntu 上跑apache2.4 的www-data 这跟身分就不能直接存取,就算把www-data 加入sudo 身分,也不能使用像是php 中的filesize() 这样的函式。

说明里头有提到可以用credential key file (json format)的方式,另外这篇文章也有提到,不过因为手上这台机器已经有做过gcloud auth login,所以没有用这样的方式。

mount as www-data identity

但很诡异的是照着文件最底下( 这边)所写的,将mount 写在/etc/fstab 却一直有问题。所以我改成写在crontab 里头,但一样要先确认www-data 的id。

 $ id --help
Usage: id [OPTION]... [USER]
Print user and group information for the specified USER,
or (when USER omitted) for the current user.

  -a ignore, for compatibility with other versions
  -Z, --context print only the security context of the process
  -g, --group print only the effective group ID
  -G, --groups print all group IDs
  -n, --name print a name instead of a number, for -ugG
  -r, --real print the real ID instead of the effective ID, with -ugG
  -u, --user print only the effective user ID
  -z, --zero delimit entries with NUL characters, not whitespace;
                   not permitted in default format
      --help display this help and exit
      --version output version information and exit

Without any OPTION, print some useful set of identified information.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report id translation bugs to <http://translationproject.org/team/>
Full documentation at: <http://www.gnu.org/software/coreutils/id>
or available locally via: info '(coreutils) id invocation'
$ id -g www-data
33
$ id -u www-data
33

然后便是在crontab 中加入@reboot 去做mount

 $ crontab -l
@reboot gcsfuse --uid 33 --gid 33 --implicit-dirs -o allow_other -file-mode=660 -dir-mode=770 BUCKET-NAME /PATH/TO/MOUNT/

但终究比较好的方式是乖乖用google cloud storage api, 这样就只要单纯deploy 程式跟credential key file 就解决,不用弄这些设定。

Original link: Phanix's Blog

CC BY-NC-ND 2.0 版权声明

喜欢我的文章吗?
别忘了给点支持与赞赏,让我知道创作的路上有你陪伴。

加载中…

发布评论